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.
Inder
--
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.