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.

Serialization issue with RuntimeTypeAdapterFactory

See original GitHub issue

Hello, i found after serialization root object ‘type’ field is not injected, for inner object ‘type’ fields are added. Example:

public class Test {
  public class Base {
    String baseField = "base";
  }
  public class Child extends Base {
    String childField = "child";
    Base baseInChild = new Base();
  }

  public static void main(String[] args) {
        Gson mGson = new GsonBuilder()
                .setPrettyPrinting()
                .registerTypeAdapterFactory(
                        RuntimeTypeAdapterFactory.
                                of(Base.class).
                                registerSubtype(Base.class).
                                registerSubtype(Child.class)
                )
                .create();

        final String jsonStr = mGson.toJson(new Child());
        final Base child = mGson.fromJson(jsonStr, Base.class); // ERROR HERE: "Caused by: com.google.gson.JsonParseException: cannot deserialize class mypackage.Test$Base because it does not define a field named type"
  }
}

and json output after serialization:

{
  "baseInChild": {
    "type": "Base",
    "baseField": "base"
  },
  "childField": "child",
  "baseField": "base"
//in this place should be generated field 'type' with value 'Child'
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8

github_iconTop GitHub Comments

33reactions
ultraoncommented, Feb 23, 2016

Hello, i’ve found bug in RuntimeTypeAdapterFactory (line185, method create(), and resolved this issue, need replace

if (type.getRawType() != baseType)

with

if (null == type || !baseType.isAssignableFrom(type.getRawType()))

Then RuntimeTypeAdapterFactory works well as expected. Full version: RuntimeTypeAdapterFactory

2reactions
swankjessecommented, Feb 15, 2016

Try replacing this:

        final String jsonStr = mGson.toJson(new Child());

With this:

        final String jsonStr = mGson.toJson(new Child(), Base.class);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Gson's RuntimeTypeAdapterFactory isn't serializing the type
Thanks to help from a contributor to the public repository, the problem was the serialization step needed to include the base type.
Read more >
Goodbye RuntimeTypeAdapterFactory. Polymorphic ...
Polymorphic serialization using kotlinx.serialization ... With Gson deserializing this is pretty easy using a RuntimeTypeAdapterFactory .
Read more >
Developers - Serialization issue with RuntimeTypeAdapterFactory -
Hello, i found after serialization root object 'type' field is not injected, for inner object 'type' fields are added. Example:
Read more >
Practical Gson — How to Deserialize a List of Polymorphic ...
Finally, I found the RuntimeTypeAdapterFactory class, which solves the problem quite nicely. Gson Series Overview. Mapping; Configuration ...
Read more >
RuntimeTypeAdapterFactory (Gson Extras version ... - javadoc.io
This class addresses this problem by adding type information to the serialized JSON and honoring that type information when the JSON is deserialized:...
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