How to simplify deserialization of class containing generic list?

1,527 views
Skip to first unread message

blep

unread,
Feb 17, 2010, 2:49:14 AM2/17/10
to google-gson
Hi,

I'm using gson to deserialize JSON data. Currently, I have to write a
JsonDeserializer for every single class that contains Collection, so
that I can specify the TypeToken for deserialization.

Is there a way to just "annotate" the field in the class (or in
GsonBuilder) to specify its TypeToken so that custom JsonDeserializer
are not required?

Here is the code I'm currently using to deserialize the simple class
below:

class ProjectTestScenario {
public String name;
public String dir;
public List<ProjectSource> sources;

static class Deserializer implements
JsonDeserializer<ProjectTestScenario> {
public ProjectTestScenario deserialize(JsonElement json, Type
typeOfT, JsonDeserializationContext context) throws JsonParseException
{
ProjectTestScenario project = new ProjectTestScenario();
JsonObject object = json.getAsJsonObject();
project.name = object.get( "name" ).getAsString();
project.dir = object.get( "dir" ).getAsString();
JsonElement jsonSources = object.get( "sources" );
Type listType = new TypeToken<List<ProjectSource>>()
{}.getType();
project.sources = context.deserialize( jsonSources,
listType );
return project;
}
}
}

Thanks,
Baptiste.

inde...@gmail.com

unread,
Feb 17, 2010, 4:43:28 PM2/17/10
to google-gson
Gson can handle deserialization of Genericized Collections so you
should not need to register a deserializer for ProjectTestScenario.
Did you try that already and ran into a problem?

Inder

Yoichi Takayama

unread,
Feb 17, 2010, 6:09:34 PM2/17/10
to googl...@googlegroups.com
I have no problem in serialising and deserialising Lists without declaring their deserializers.

I am only registering the Type (listType) with Gson. I give it as an argument to gson.toJasone(jsonString, listType).

As showin in:


and this may be also of help:


Does this help?

Apparently there is some deserialisation  problem if the List is typed with an interface, because it has no field to serialise. In such a case, probably you have to specify a concrete class that is implementing the interface.

Yoichi

--
You received this message because you are subscribed to the Google Groups "google-gson" group.
To post to this group, send email to googl...@googlegroups.com.
To unsubscribe from this group, send email to google-gson...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-gson?hl=en.


Baptiste Lepilleur

unread,
Feb 26, 2010, 3:32:23 AM2/26/10
to googl...@googlegroups.com
You are correct. I needed to wrote a deserializer by hand to handle my root class, and reading the doc though that you had to deserialize all generic collection by hand.

But you are correct, this is not need. I removed my deserializer and it works fine.

This makes gson perfect for deserializing object trees.

Thanks,
Baptiste.

2010/2/18 Yoichi Takayama <takayam...@gmail.com>
Reply all
Reply to author
Forward
0 new messages