gson does not deserialize interfaces, even with an InstanceCreator
See original GitHub issueWhat 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:
- Created 9 years ago
- Reactions:2
- Comments:15
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This issue schould be prioritized. I need it soon 👍
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