-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Closed
Description
When deserialize with type of object[], gson cannot get the right type.
Code like :
@Test
public void testGsonObjectArray() throws Exception{
Gson gson = new Gson();
Object[] objects = new Object[2];
objects[0] = 100;
objects[1] = new Inner(1);
Outer outer = new Outer(1, "outer-instance1", objects);
String jsonString = gson.toJson(outer);
System.out.println("Gson#toJson result :" + jsonString);
Outer outerDeser = gson.fromJson(jsonString, Outer.class);
System.out.println("Gson#fromJson result : " + outerDeser);
System.out.println(outerDeser.objects[0].getClass());
System.out.println(outerDeser.objects[1].getClass());
}
class Outer{
private int id;
private String name;
private Object [] objects;
public Outer(int id, String name, Object[] objects) {
this.id = id;
this.name = name;
this.objects = objects;
}
@Override
public String toString() {
return "Outer{" +
"id=" + id +
", name='" + name + '\'' +
", objects=" + Arrays.toString(objects) +
'}';
}
}
class Inner{
private int id;
public Inner(int id) {
this.id = id;
}
}
The test result is :
Gson#toJson result :{"id":1,"name":"outer-instance1","objects":[100,{"id":1}]}
Gson#fromJson result : Outer{id=1, name='outer-instance1', objects=[100.0, {id=1.0}]}
class java.lang.Double
class com.google.gson.internal.LinkedTreeMap
Is there any way to handle this?
Metadata
Metadata
Assignees
Labels
No labels