Closed GoogleCodeExporter closed 9 years ago
I add something similar to your problem, issue 209.
I "fix" your problem as following on version 1;3 (1.4 is beta did not look at
it):
Class: com.google.gson.JsonSerializationVisitor
Method: visitArray
Old content
public void visitArray(Object array, Type arrayType) {
assignToRoot(new JsonArray());
int length = Array.getLength(array);
TypeInfoArray fieldTypeInfo = TypeInfoFactory.getTypeInfoForArray(arrayType);
Type componentType = fieldTypeInfo.getSecondLevelType();
for (int i = 0; i < length; ++i) {
Object child = Array.get(array, i);
Type childType = componentType;
// we should not get more specific component type yet since it is possible
// that a custom
// serializer is registered for the componentType
addAsArrayElement(new ObjectTypePair(child, childType, false));
}
}
New content
public void visitArray(Object array, Type arrayType) {
assignToRoot(new JsonArray());
int length = Array.getLength(array);
for (int i = 0; i < length; ++i) {
Object child = Array.get(array, i);
// we should not get more specific component type yet since it is possible
// that a custom
// serializer is registered for the componentType
addAsArrayElement(new ObjectTypePair(child, child.getClass(), false));
}
}
Etienne
Original comment by lapinouj...@gmail.com
on 15 May 2010 at 1:02
Sorry my thread was for the current version in trunk.
For version 1.3 I did the folowwing:
Class: com.google.gson.JsonSerializationVisitor
Method: visitArray
Old content
public void visitArray(Object array, Type arrayType) {
assignToRoot(new JsonArray());
int length = Array.getLength(array);
TypeInfoArray fieldTypeInfo = TypeInfoFactory.getTypeInfoForArray(arrayType);
Type componentType = fieldTypeInfo.getSecondLevelType();
for (int i = 0; i < length; ++i) {
Object child = Array.get(array, i);
addAsArrayElement(componentType, child);
}
}
New content
public void visitArray(Object array, Type arrayType) {
assignToRoot(new JsonArray());
int length = Array.getLength(array);
for (int i = 0; i < length; ++i) {
Object child = Array.get(array, i);
addAsArrayElement(child.getClass(), child);
}
}
Etienne
Original comment by lapinouj...@gmail.com
on 15 May 2010 at 1:10
Original comment by inder123
on 3 Nov 2010 at 1:48
FYI - you can work around this by providing static type information when
calling toJson().
toJson(array, Integer[][].class);
In general GSON works better when it knows the types of everything!
Original comment by limpbizkit
on 3 Nov 2010 at 4:43
@limpbizkit: Understood.
In the situation that led to this report, those arrays were buried deep within
a Map<String, Object>, which contained many nested maps, collections, and
arrays of various types. I was building this data structure up in Java and
wanted to convert it to JSON in one pass, so that solution would not have been
practical for me.
Original comment by michael.hixson@gmail.com
on 3 Nov 2010 at 5:44
Yup, good to know. One thing we should consider doing is using runtime types
whenever "Object" is the static type.
Original comment by limpbizkit
on 3 Nov 2010 at 5:54
Original comment by limpbizkit
on 4 Nov 2010 at 10:52
Issue 245 has been merged into this issue.
Original comment by limpbizkit
on 4 Nov 2010 at 10:53
I have an inkling that this works in Gson 1.7.
We have a test for a Collection<Object>, Object[] and a Map<String,Object>.
Original comment by joel.leitch@gmail.com
on 13 Apr 2011 at 9:16
Original issue reported on code.google.com by
michael.hixson@gmail.com
on 23 Apr 2010 at 10:51