AllenCoder / google-gson

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

GenericType convert problem #328

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.A class has an Object field:
public class ClassA{
    Object o;

    public Object getO() {
        return o;
    }

    public void setO(Object o) {
        this.o = o;
    }
}

2. a GenericClass like this:

public class ClassB<T> {
    T f;

    public T getF() {
        return f;
    }

    public void setF(T f) {
        this.f = f;
    }
}

3. convert a ClassA object:

ClassA obja = new ClassA();
ClassB<String> objb = new ClassB<String>();
objb.setF("this is objB");

List<ClassB<String>> objblist = new ArrayList<ClassB<String>>();
objblist.add(objb);
obja.setO(objblist);

System.out.println(g.toJson(obja));

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

expected output: {"o":[{"f":{}}]}
actually output: {"o":[{"f":"this is objB"}]}

What version of the product are you using? On what operating system?
version: google-gson-1.7.0
jdk:1.6_20
os:Windows 7

Original issue reported on code.google.com by liu78...@gmail.com on 18 May 2011 at 5:35

GoogleCodeExporter commented 9 years ago
This is indeed the expected output with Gson. During serialization, it writes 
out the actual object's fields instead of the specified type.

Original comment by inder123 on 20 May 2011 at 9:32

GoogleCodeExporter commented 9 years ago
You can register a type hierarchy adapter for your types that limits the output 
to the specific set of fields.

Original comment by inder123 on 20 May 2011 at 9:37

GoogleCodeExporter commented 9 years ago
Register a type adapter need to know the type - the type of ClassB's field f.
Actually i do not know the type. How do i do?

Original comment by liu78...@gmail.com on 24 May 2011 at 6:08