question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support to look ahead / go backward / polymorphic JSON

See original GitHub issue

The 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:

  1. 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
  2. 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:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
JakeWhartoncommented, Jun 3, 2016

Here’s a full example of polymorphic array deserialization I helped with: http://stackoverflow.com/a/36784255/132047

0reactions
danielesegatocommented, Jun 3, 2016

Oh I see thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserialize JSON with Jackson into Polymorphic Types
Option 2: Support polymorphic serialization / deserialization for abstract classes (and Object typed classes), and arrays of those types.
Read more >
Deserializing JSON into polymorphic classes with System.Text ...
In this post, I'll explain how to write a custom JsonConverter for System.Text.Json to help with deserialization for such cases.
Read more >
Dynamic JSON deserialization of complex polymorphic data ...
In our project we faced a challenge regarding JSON deserialization. We were a consumer of a predefined polymorphic JSON data entities.
Read more >
JSON polymorphism in Go. Serializing and deserializing…
To do so, we will serialize each item in Vehicles and copy it to RawVehicles . Then let json. Marshal do what it...
Read more >
Polymorphic JSON Serialization (feat. .NET & System.Text.Json)
Time for one more video on messing with System.Text. Json, this time to get polymorphic (de)serialization going.This is a simple approach, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found