jsoniter-scala icon indicating copy to clipboard operation
jsoniter-scala copied to clipboard

migration from dsl-json (how to parse a string into a stream of tokens)

Open ghost opened this issue 3 years ago • 6 comments

i'm trying to migrate my code from dsl-json to jsoniter-scala. In all the examples I've seen there are codecs to parse the string into a domain object. I'm getting stuck trying to parse a string or array of bytes. I want to follow the approach of getting a token at a time using the API methods like readInt, readBoolean or readKey.

Is it possible? where can i find an example?

thanks for your time! regards

ghost avatar Jul 12 '22 17:07 ghost

@imrafaelmerino Could you please give more context to avoid possible the XY problem? Do you have an option to share your existing code?

Benchmarks can be good examples of migration from DSL-JSON to jsoniter-scala.

To see sources of codecs generated by jsoniter-scala-macros you can add -Dmacro.settings=print-codecs option like here.

plokhotnyuk avatar Jul 12 '22 17:07 plokhotnyuk

Thanks for the quick response.

Basically, I create a JsonReader from an array of bytes or a string. Then I call the method getNextToken in a while loop to process the whole Json.

Take a look at the method

JsObj value(final JsonReader<?> reader) throws IOException

in

example

imrafaelmerino avatar Jul 12 '22 17:07 imrafaelmerino

@imrafaelmerino Please check if other examples that parse JSON to some AST using Core API could be more helpful:

plokhotnyuk avatar Jul 13 '22 10:07 plokhotnyuk

just take a look. This is what I'm looking for.

just like I did in https://github.com/imrafaelmerino/json-values with dsl-json, I think I'll be able to interleave parsing and validation which turns out to be very efficient (all other libraries parse the whole json and then validate it...)

really appreciate your help.

imrafaelmerino avatar Jul 13 '22 10:07 imrafaelmerino

If the structure of JSON is known then most efficient way to parse, validate, and serialize is doing that immediately to users data structures: (case) classes and collections. Macro API of jsoniter-scala auto-derives super-efficient codecs so no need to waste CPU clocks for instantiation of string for keys nor boxing of primitive values nor creation of hash maps for key-value pairs nor redundant traverse of hash maps for validation.

Transformation is also more efficient between data structures immediately (not their parsed JSON representation). You can use chimney macros to reduce boilerplate.

plokhotnyuk avatar Jul 13 '22 10:07 plokhotnyuk

I'll take a look at the macros you mentioned.

Let me elaborate on my idea. First, I don't use domain objects at all. In my scenario, I work with Json from end to end. Think of a message-passing system like Vertx, where Json is the lingua Franca, and you have to persist data into MongoDB which works with Json as well.

The thing is I want to validate my data using just predicates. For doing that I create what I call a spec (inspiration came from Clojure language):

JsObjSpec( "a" -> int(it => it >0), "b" -> str(it => it.length > 10) )

From a spec I create a parser using dsl-json (what I want to migrate in the scala library to jsonniter). This parser, as soon as it parses a value of any field a or b, validates it against its spec predicate. After all, failing fast is important too!

It's more efficient than other json-schema implementations and defining specs is as easy as defining json since they have the same structure.

thanks again for your time

imrafaelmerino avatar Jul 13 '22 10:07 imrafaelmerino