Pablo
Pablo

Reputation: 3467

Gson deserialize Collections

New versions of gson have a lot of changes regarding deserialization of Collections. Before we used TypeToken in order to retrieve a List from a Json Array, however TypeToken no longer exists in the library. Are there any examples of what approach to take now??... have been looking at the documentation but it's no clear about this. It suggested implementing the JsonDeserializer interface, and using GsonBuilder... buit again it's not clear. Does anybody has an example of how could we deserialize a collection by using Gson?. Thanks a lot.

Upvotes: 2

Views: 2350

Answers (1)

Pablo
Pablo

Reputation: 3467

Version 2.1 of google Gson no longer has a public constructor for the TypeToken class, instead we should use one of the static methods provided by it:

IE:

List<MyClass> myList= new ArrayList<MyClass>();
Type listType = TypeToken.get(myList.getClass()).getType();
myList= (new Gson()).fromJson(jsonString, listType);

Upvotes: 2

Related Questions