Add support to look ahead / go backward / polymorphic JSON
See original GitHub issueThe issue
The Gson parser only allow parsing the json if you know before you are parsing it which object each field contain.
If a field contains polymorphic data it can’t be parsed with GSon.
As by JSON specificaiton: “An object is an unordered set of name/value pairs.” Thus being forced to know the order of the fields to parse the JSON becames a problem.
Examples:
- you have a type field and you need to change the object type to of the Object (and thus the parser) given that field value
- you need to adapt the way you parse the object given some field of the JSon within the data itself.
Possible solutions? (what would work for me)
The choice of parsing the object with a cursor is extremely efficient, I agree with that, but I think we, developer, should have a way of also handling polymorphic data. Even if I have to give away some performance for that portion of the JSON.
Currently I’m forced to parse the object into a Map and then manually create my objects, which is really bad in performance and Object allocation anyway.
I still like GSon better then alternatives and I would love if it could implement one or more of these features (in order or preference):
A) Go Backward
Allow placing marks in the JSonParser:
in.mark("name");
// do stuff (beginObject(), next() etc..)
in.resetTo("name");
B) natively support parsing into an org.json JsonObject / JsonArray
Add
in.nextJsonObject();
and
in.nextJsonArray();
to consume a value.
this is far for optimum but at least gives a way of handling cases like this with a custom TypeAdapter.
This require to keep some kind of buffer or use an InputStream / Reader that support marking.
C) Look Ahead
Using JSON Path getting a specific value inside the JSON This gives away the performance inside that particular portion of the JSON of course because it has to be parsed completely into a JSON object and then the JsonParser will just visit an already parsed object.
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
Here’s a full example of polymorphic array deserialization I helped with: http://stackoverflow.com/a/36784255/132047
Oh I see thank you!