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.

gson does not deserialize interfaces, even with an InstanceCreator

See original GitHub issue
What steps will reproduce the problem?

1. Create an interface that defines getters only
2. Create a concrete class that implements the interface, with local fields and 
accessors corresponding to the interface
3. Specify an InstanceCreator to create concrete instances of the interface
4. Serialize a concrete instance of the class using gson
5. Deserialize from the JSON string produced in step 4.

What is the expected output? What do you see instead?

Given that an InstanceCreator is specified, I would expect a concrete 
implementation of the interface corresponding to the type created in the 
InstanceCreator, with the fields set from the JSON string.

Instead, a concrete instance of the class is returned but the fields are NOT 
populated.

Using version 2.1

I get that this can be done by creating a custom serializer/deserializer for 
the type, but I don't get why that should be required.  If so I can just use a 
regular JSONObject and do it all myself.  Seems like it should be a fairly 
simple task.  Take the runtime type created by the InstanceCreator, match the 
fields in the instance against the values in the JSON string using reflection, 
set and repeat.

Am I missing something here?

Original issue reported on code.google.com by jason.po...@gmail.com on 21 Feb 2012 at 2:33

Issue Analytics

  • State:open
  • Created 9 years ago
  • Reactions:2
  • Comments:15

github_iconTop GitHub Comments

8reactions
bes1002tcommented, Mar 9, 2016

This issue schould be prioritized. I need it soon 👍

3reactions
volocoptercommented, Sep 28, 2015

This is a major bug. I have wasted 2 days into this till I came across this page. Google, please solve this problem or at least give an easier workaround

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserializing Json String containing interfaces not working in ...
Register an InstanceCreator with Gson for this type may fix this problem. I tried to register the type adapter for an instance creator...
Read more >
How To Serialize And Deserialize Interfaces In Java Using Gson
Register an InstanceCreator with Gson for this type may fix this problem." JSON Serialize. Gson doesn't have way to identify which "Car Object" ......
Read more >
Gson User Guide - Google Sites
Gson can serialize static nested classes quite easily. Gson can also deserialize static nested classes. However, Gson can not automatically deserialize the pure ......
Read more >
Custom Interface Adapter To Serialize And Deserialize ...
Register an InstanceCreator with Gson for this type may fix this problem.” Solution. The below solution is the subject of this post. What...
Read more >
Serialization of Java objects using Google's Gson library
In fact, Gson can deserialize a class even without a no-args constructor or registered InstanceCreator . Try removing the no-args constructor from the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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