google / google/gson

gson does not deserialize interfaces, even with an InstanceCreator

Open
#411 6 comments 2 reactions 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

```
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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.