Deserialization problem

769 views
Skip to first unread message

tips

unread,
Feb 3, 2009, 1:14:42 AM2/3/09
to google-gson
Hi all,

I've got a List of complex objects which I cannot deserialize.

I get the following error:

Caused by: java.lang.IllegalArgumentException: Collection objects need
to be parameterized unless you use a custom serializer. Use the
com.google.gson.reflect.TypeToken to extract the ParameterizedType.
at com.google.gson.TypeInfoCollection.<init>(TypeInfoCollection.java:
34)

I have no problem serializing.

I can deserialize using json-lib (net.sf.json.JSONSerializer.toJSON)
but not using gson.

//create a list of myObect's
List<myObject> myObjectList = new ArrayList();
myObjectList.add(new myObject());
myObjectList.add(new myObject());
myObjectList.add(new myObject());

//serialize, this works fine
Gson gson = new Gson();
String json = gson.toJson(myObjectList);

//Now deserialize......

Type listType = new TypeToken<List<myObject>>() {}.getType();
List<myObject> output1 = gson.fromJson(json, collectionType); //this
fails

Type collectionType = new TypeToken<Collection<myObject>>(){}.getType
();
List<myObject> output2 = gson.fromJson(json, collectionType); //this
also fails

//but if I use json-lib
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setArrayMode(JsonConfig.MODE_LIST);
jsonConfig.setRootClass(myObject);
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( jsonObj,
jsonConfig);
List<myObject> output3 = (List<myObject>) JSONSerializer.toJava
( jsonArray, jsonConfig ); //this works fine

Am I using gson incorrectly?

Thanks,
T

inde...@gmail.com

unread,
Feb 4, 2009, 8:02:06 PM2/4/09
to google-gson
Your code seems fine, and should work. We have several tests that test
this kind of functionality.

Have you been able to deserialize just the myObject instances?

If you can post a test that fails, we will be happy to debug it and
see why it is failing.

Thanks
Inder

tips

unread,
Feb 7, 2009, 8:05:49 PM2/7/09
to google-gson
Thanks for the offer to debug. I've looked further into it and have
found the problem.

The object being serialized/ deserialized contains a collection of
objects of the same type. It doesn't use generics so I assume Gson
doesn't know the type and hence throws the error about the child
collection and not the root collection as I presumed.

e.g.

public class myObject {

//... other primitive properties ....
//....

private List childObjects= new ArrayList();

private List childObjects = new ArrayList();
public void addChildObjects(myObject m) {
childObjects.add(m);
}
public List getChildObjects() {
return childObjects;
}
public void setChildObjects(List childObjects) {
this.childObjects = childObjects;
}
}


I suppose to get around this I would have to write a custom
deserializer but I assume that would mean traversing through each
property of the root object which could potentially fall out of synch
with the deserializer. My laziness means I will stick with json-lib
for deserialization.

Obviously generics would help but the code with myObject isn't Java
1.5 compatible yet.

The reason json-lib works is because it creates a DynaBean as it
doesn't know the type of the collection either. However I can tell it
what type to create using a simple config element as follows (for the
above example):

Map<String, Class> classMap = new HashMap<String, Class>();
classMap.put("childObjects", myObject.class);
jsonConfig.setClassMap( classMap );


Any plans of adding something similar to Gson in the future?

Thanks,
T
Reply all
Reply to author
Forward
0 new messages