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<MyClass>() {}.getClass()); But then I get an error in Eclipse:
"The type new
List<MyClass>() {}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<List<MyClass>>() {}.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) - rawTypeName = "java.util.ArrayList" So it seems the getClass invocation didn't work. The Gson User Guide mentions a runtime exception that should happen during parsing a generic type to JSON. I did it wrong (not shown above) just as in the example, but didn't get that exception. So I changed the serialization as the user guide suggested. Didn't help though.
TokenType. Have you tried that way?