-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Closed
Description
Assume you have the following 2 classes:
class Entity<IDT,NameT>{
IDT id;
NameT name;
public Entity(IDT id, NameT name) {
super();
this.id = id;
this.name = name;
}
public IDT getId() {
return id;
}
public NameT getName() {
return name;
}
}
class Employee extends Entity<Integer,String>{
double age;
private int tag;
public Employee() {
super(0,null);
}
public Employee(int id, String name, double age) {
super(id,name);
this.age = age;
}
public double getAge() {
return age;
}
void setTag(int tag){
this.tag = tag;
}
}
when trying to use
new GsonBuilder().create().toJson(new Employee(1,"Mary",10)); it throws
java.lang.UnsupportedOperationException: Expecting parameterized type, got
class test.gson.Employee.
Are you missing the use of TypeToken idiom?
See
http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializ
ing-Gener
ok, then I try the generic one using
new GsonBuilder().create().toJson(new Employee(1,"Mary",10),new
TypeToken<Employee>(){}.getType()); Gson will only show id/name field, all
other field in Employee class is ignored. any solution?
I've refered <a
href="http://code.google.com/p/google-gson/source/browse/trunk/gson/src/test/jav
a/com/google/gson/functional/ParameterizedTypesTest.java">,
but didn't help.
please help to solve the problem. thanks!
Original issue reported on code.google.com by hnjch...@gmail.com
on 26 Oct 2009 at 8:49