Adding a deserializer changes serialization !?

70 views
Skip to first unread message

Fabian Zeindl

unread,
Oct 29, 2012, 9:09:13 AM10/29/12
to googl...@googlegroups.com
Hi,

 i have the following gson config:



GsonBuilder builder = new GsonBuilder();

builder.setPrettyPrinting();

builder.serializeNulls();

builder.enableComplexMapKeySerialization();

builder.registerTypeAdapter(ImmutableList.class, new ImmutableListDeserializer());

builder.registerTypeAdapter(ImmutableMap.class, new ImmutableMapDeserializer());

builder.registerTypeAdapter(ImmutableSet.class, new ImmutableSetDeserializer());

builder.registerTypeAdapter(Document.class, new PropertyBasedInterfaceDeserializer());

builder.registerTypeAdapter(RMIAnswer.class, new RMIAnswer.Deserializer());

builder.registerTypeAdapter(Article.class, new PropertyBasedInterfaceDeserializer());


For some reason, when adding the Deserializer (!) for Document.class (which is abstract), only the members of the abstract class are serialized, not the members of the actual type.

I have no custom serializer or TypeAdapters anywhere in my code.


What am i doing wrong?

Thank you

- fabian

Jesse Wilson

unread,
Oct 29, 2012, 12:36:13 PM10/29/12
to googl...@googlegroups.com
I suspect you're hitting a weird edge case in the code that adapts JsonSerializer and JsonDeserializer to work within the streaming backend. We've got some rather ugly code in TypeAdapterRuntimeTypeWrapper that attempts to Do-What-You-Want when the runtime type disagrees with the static type, and it probably isn't working in your case.

  public void write(JsonWriter out, T value) throws IOException {
    // Order of preference for choosing type adapters
    // First preference: a type adapter registered for the runtime type
    // Second preference: a type adapter registered for the declared type
    // Third preference: reflective type adapter for the runtime type (if it is a sub class of the declared type)
    // Fourth preference: reflective type adapter for the declared type

I've added a test but I couldn't reproduce the behavior you're running into. Could you please isolate the problem in a test like this one ? Once we have that we can decide what our next steps should be.

Thanks Fabian.

Fabian Zeindl

unread,
Oct 29, 2012, 2:23:09 PM10/29/12
to googl...@googlegroups.com
Here's the test-case: http://pastie.org/5133581

Fabian Zeindl

unread,
Nov 18, 2013, 1:17:23 PM11/18/13
to googl...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages