Moshi: JsonReader.readJsonString

Created on 19 Sep 2018  Â·  14Comments  Â·  Source: square/moshi

The opposite of JsonWriter.value(BufferedSource) which allows you to read a value as raw JSON. It can return either a String or ByteString depending on the ergonomics you want.

It can be implemented by peek()-ing the source, calling skipValue(), and then taking the offset you stopped at and calling readUtf8/readByteString with that offset (as a length) on the original source.

Most helpful comment

From an API standpoint, should we offer something higher level to opt this in?

class Foo(
  @JsonString val rawString: String // or ByteString
)

All 14 comments

This could also be implemented today by implementing the skip methods to take in a BufferedSink and writing to the sink instead of skipping as the skip methods read.

The tricky part in either implementation case is that peeking goes ahead and disposes of the bytes for things like opening brackets, whitespace, and the like.

ByteString JsonReader.readJsonString() or void JsonReader.readJsonValue(BufferedSink)?
the former being the very common case, the latter being more flexible.

Probably the first one

From an API standpoint, should we offer something higher level to opt this in?

class Foo(
  @JsonString val rawString: String // or ByteString
)

I'm likely going to add JsonReader.valueSource() for symmetry with JsonWriter.valueSink() that's also coming. This means if you want an arbitrary JSON value you can do valueSource().readByteString() or valueSource().readUtf8String() or JsonReader.of(valueSource()) for a ByteString, String, or JsonReader, respectively.

I don't think this is a common enough occurrence to warrant its own annotation. It can start as a sample, perhaps.

👌 should we hold off on the existing jsonString() PRs in favor of that
then?

On Fri, Sep 27, 2019 at 7:54 PM Jake Wharton notifications@github.com
wrote:

I'm likely going to add JsonReader.valueSource() for symmetry with
JsonWriter.valueSink() that's also coming. This means if you want an
arbitrary JSON value you can do valueSource().readByteString() or
valueSource().readUtf8String() or JsonReader.of(valueSource()) for a
ByteString, String, or JsonReader, respectively.

I don't think this is a common enough occurrence to warrant its own
annotation. It can start as a sample, perhaps.

—
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/675?email_source=notifications&email_token=AAKMJPQC3LRBU37F6NN3TQ3QL2MMPA5CNFSM4FWAYCK2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD72LD6A#issuecomment-536130040,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAKMJPS7DMBCNP3B4XX7UHDQL2MMPANCNFSM4FWAYCKQ
.

Hi @JakeWharton, as this issue is open since 2018, does we have any solution to get a raw json from the JsonReader?

I'm likely going to add JsonReader.valueSource() for symmetry with JsonWriter.valueSink() that's also coming.

It looks like there was an implementation in https://github.com/square/moshi/pull/954 but it was never merged?

I really like the @JsonString val rawString: String suggestion above, it would perfectly solve the exact problem that I am running into now in my project. We are incrementally migrating from jackson to moshi and this is one of the things currently getting in our way.

Any updates on when this addition might be available?

Thanks for the wonderful library!

That is perhaps a high-level interface to the underlying feature we need to build, but it's not strictly what this issue is tracking since half of the behavior needed to implement that has already landed.

Should I re-kick the PR I started?

If you want. My branch was on my Google computer which I burned in a dumpster behind the local Wendy's.

On Thu, Sep 10, 2020, at 9:22 PM, Zac Sweers wrote:

Should I re-kick the PR I started?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub https://github.com/square/moshi/issues/675#issuecomment-690817078, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAQIEP4CKVFKW6EB4CLNSDSFF3WTANCNFSM4FWAYCKQ.

Alright rehashed in #1219, reviews welcome!

Thanks guys, this will be helpful.

Was this page helpful?
0 / 5 - 0 ratings