Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding deserializer for interfaces causes serialization to fail #479

Closed
GoogleCodeExporter opened this issue Mar 19, 2015 · 2 comments
Closed
Labels

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?
1.Create a deserializer for an interface class and register it
2.Serialization of the interface no longer occurs.

What is the expected output? What do you see instead?
I expected to see the class serialized like it was before the deserializer was 
registered.


What version of the product are you using? On what operating system?
Using GSON version 2.1, on Windows 7 x64, using Oracle Java (32 bit) 7 update 3

Please provide any additional information below.

Here is a small example that will illustrate the issue (written as a JUnit 
test).


    public interface HelloInterface {
        public String sayHello();
    }

    public class HelloClass implements HelloInterface{
        private String hi = "hi";

        public String sayHello(){
            return hi;
        }
    }

    public class Greeter {

        private HelloInterface saysHello;

        public void setHello(HelloInterface hello){
            saysHello = hello;
        }

    }

    public void testGsonSerialization(){
        GsonBuilder builder = new GsonBuilder();
        Greeter greeter = new Greeter();
        final String expectedCorrect = "{\"saysHello\":{\"hi\":\"hi\"}}";
        final String incorrectJson = "{\"saysHello\":{}}";
        // Should be able to serialize, but not deserialize.
        greeter.setHello(new HelloClass());
        String jsonResult = builder.create().toJson(greeter);
        assertEquals(expectedCorrect, jsonResult);
        builder.registerTypeAdapter(HelloInterface.class, new JsonDeserializer<HelloInterface>() {

            @Override
            public HelloInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
                // Do stuff, it doesn't even matter here.
                return null;
            }
        });
        //Registering a deserializer causes the serialization to not work anymore.
        String secondJsonResult = builder.create().toJson(greeter);
        // This line passes.
        assertEquals(incorrectJson, secondJsonResult);
        // This line fails
        assertEquals(expectedCorrect, secondJsonResult);
    }

Original issue reported on code.google.com by [email protected] on 9 Oct 2012 at 9:02

@Marcono1234
Copy link
Collaborator

Still the case in Gson 2.9.0, and seems to be the same as #543 / #2032.

@Marcono1234 Marcono1234 added the bug label Aug 3, 2022
@Marcono1234
Copy link
Collaborator

Looks like #1787 fixed this issue. The next Gson version will include that fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants