discomarathon / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Can not serialize when the bean contains a List of object defined by interfaces #209

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
See the code:

public class FirstImpl implements First {

    private String name;

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

public interface First {

    public String getName();

    public void setName(String name);

}

import java.util.List;

public class ParentFirst {

    private String name;
    private List<First> firsts;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public List<First> getFirsts() {
        return this.firsts;
    }

    public void setFirsts(List<First> firsts) {
        this.firsts = firsts;
    }
}

import java.util.List;

public class ParentFirstImpl {

    private String name;
    private List<FirstImpl> firsts;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public List<FirstImpl> getFirsts() {
        return this.firsts;
    }

    public void setFirsts(List<FirstImpl> firsts) {
        this.firsts = firsts;
    }
}

Gson gson = new Gson();
String json = null;

FirstImpl firstClass = new FirstImpl();
firstClass.setName("First name by class");
json = gson.toJson(firstClass);  
System.out.println(json);

First firstInterface = new FirstImpl();
firstInterface.setName("First name by interface");
json = gson.toJson(firstInterface);  
System.out.println(json);

List<FirstImpl> firstsClass = new ArrayList<FirstImpl>();
firstsClass.add(firstClass);
json = gson.toJson(firstsClass);
System.out.println(json);

List<First> firstsInterface = new ArrayList<First>();
firstsInterface.add(firstInterface);
json = gson.toJson(firstsInterface);
System.out.println(json);

ParentFirst parentFirst = new ParentFirst();
parentFirst.setName("List first name by interface");
parentFirst.setFirsts(firstsInterface);
json = gson.toJson(parentFirst);
System.out.println(json);

ParentFirstImpl parentFirstImpl = new ParentFirstImpl();
parentFirstImpl.setName("List first name by class");
parentFirstImpl.setFirsts(firstsClass);
json = gson.toJson(parentFirstImpl);
System.out.println(json);

What is the expected output? What do you see instead?
Expected:
[{"name":"First name by class"}]
[{"name":"First name by interface"}]
{"name":"List first name by interface","firsts":[{"name":"First name by
class"}]}
{"name":"List first name by class","firsts":[{"name":"First name by class"}]}

See:
[{"name":"First name by class"}]
[{"name":"First name by interface"}]
{"name":"List first name by interface","firsts":[{}]}
{"name":"List first name by class","firsts":[{"name":"First name by class"}]}

What version of the product are you using? On what operating system?
1.3

Please provide any additional information below.
I am actually debugging to see if I can find a patch, even if i write a
custom serializer should work.

Original issue reported on code.google.com by lapinouj...@gmail.com on 14 May 2010 at 10:35

GoogleCodeExporter commented 9 years ago
Find a quick patch for version 1.3 and for current source. But I did not already
understood everything (just 2h on the project).

Then for version 1.3:
Class: com.google.gson.JsonSerializationVisitor
Method: visitFieldUsingCustomHandler
Line 148 (may change)
Replace: JsonElement child = serializer.serialize(obj, actualTypeOfField, 
context);
by: JsonElement child = serializer.serialize(obj, obj.getClass(), context);

For current version from svn:
Class: com.google.gson.JsonSerializationVisitor
Method: visitFieldUsingCustomHandler
Line 203
Replace: ObjectTypePair objTypePair = new ObjectTypePair(obj, 
declaredTypeOfField,
false);
by: ObjectTypePair objTypePair = new ObjectTypePair(obj, obj.getClass(), false);

I ran Junit test before and after the modification.
I got 3 errors and 6 failures, but they are the same. So the modification may 
not
have consequence.

Is the patch correct ?

Original comment by lapinouj...@gmail.com on 14 May 2010 at 11:47

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 6 Oct 2010 at 6:35