Deserialize a Listlt;Tgt; object with Gson?(反序列化列表lt;Tgt;反对Gson?)
问题描述
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 (BalusC's answer):
MyClass mc = new Gson().fromJson(result, new List<MyClass>() {}.getClass());
but then I get an error in Eclipse saying "The type new List<MyClass>() {}
must implement the inherited abstract method..." and if I use a quick fix I get a monster of over 20 method stubs.
I am pretty sure that there is an easier solution, but I seem unable to find it!
Now I have this:
Type listType = new TypeToken<List<MyClass>>() {}.getType();
MyClass mc = new Gson().fromJson(result, listType);
However, I do 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 do catch JsonParseExceptions
and result
is not null.
I checked listType
with the debugger and got the following:
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 properly. Any suggestions...?
I've checked on the Gson User Guide. It 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 at all. So I changed the serialization as in the user guide suggested. Didn't help, though.
Edit:
Solved, see my answer below.
Method to deserialize generic collection:
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);
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 <
and >
) 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<List<T>>() {}.getType()
for a type parameter T
.)
For more information, see the documentation for the TypeToken
class.
这篇关于反序列化列表<T>反对Gson?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:反序列化列表<T>反对Gson?
基础教程推荐
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01