HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaCritical

Deserialize a List<T> object with Gson

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
objectdeserializewithlistgson

Problem

I want to transfer a list object via Google Gson, but I don't know how to deserialize generic types. What I tried after looking at this answer:

MyClass mc = new Gson().fromJson(result, new List() {}.getClass());


But then I get an error in Eclipse:

"The type new List() {} must implement the inherited abstract method..."

And if I use a quick fix I get over 20 method stubs. I am sure there is an easier solution. Now I have this:

Type listType = new TypeToken>() {}.getType();

MyClass mc = new Gson().fromJson(result, listType);


I get the following exception at the fromJson line:

java.lang.NullPointerException
at org.apache.harmony.luni.lang.reflect.ListOfTypes.length(ListOfTypes.java:47)
at org.apache.harmony.luni.lang.reflect.ImplForType.toString(ImplForType.java:83)
at java.lang.StringBuilder.append(StringBuilder.java:203)
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:56)
at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:76)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:106)
at com.google.gson.JsonDeserializationContextDefault.fromJsonArray(JsonDeserializationContextDefault.java:64)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:49)
at com.google.gson.Gson.fromJson(Gson.java:568)
at com.google.gson.Gson.fromJson(Gson.java:515)
at com.google.gson.Gson.fromJson(Gson.java:484)
at com.google.gson.Gson.fromJson(Gson.java:434)


I catch JsonParseExceptions and result is not null. I checked listType with the debugger and got:

```
  • list Type


- args = ListOfTypes
- list = null
- resolvedTypes = Type[ 1 ]
- loader = PathClassLoader
- ownerType0 = null
- ownerTypeRes = null
- rawType = Class (java.util.ArrayList)
- rawType

Solution

Method to deserialize generic collection:

import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;

...

Type listType = new TypeToken>(){}.getType();
List yourClassList = new Gson().fromJson(jsonArray, listType);


Since several people in the comments have mentioned it, here's an explanation of how the TypeToken class is being used. The construction new TypeToken() {}.getType() captures a compile-time type (between the `) into a runtime java.lang.reflect.Type object. Unlike a Class object, which can only represent a raw (erased) type, the Type object can represent any type in the Java language, including a parameterized instantiation of a generic type.

The
TypeToken class itself does not have a public constructor, because you're not supposed to construct it directly. Instead, you always construct an anonymous subclass (hence the {}, which is a necessary part of this expression).

Due to type erasure, the
TypeToken class is only able to capture types that are fully known at compile time. (That is, you can't do new TypeToken>() {}.getType() for a type parameter T.)

For more information, see the documentation for the
TypeToken` class.

Code Snippets

import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;

...

Type listType = new TypeToken<ArrayList<YourClass>>(){}.getType();
List<YourClass> yourClassList = new Gson().fromJson(jsonArray, listType);

Context

Stack Overflow Q#5554217, score: 1125

Revisions (0)

No revisions yet.