Google Gson - 反序列化列表< class>目的? (通用型) [英] Google Gson - deserialize list<class> object? (generic type)

查看:95
本文介绍了Google Gson - 反序列化列表< class>目的? (通用型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Google Gson传输一个列表对象,但我不知道如何反序列化泛型类型。

我在看完
$ p $ MyClass mc = new Gson() .fromJson(result,new List< MyClass>(){}。getClass());

但是后来在eclipse中出现错误,提示新的List(){}继承的抽象方法......,如果我使用快速修复,我会得到超过20个方法存根的怪物。



我很确定有一个更简单的解决方案,但我似乎无法找到它!



编辑:



现在我有

 输入listType = new TypeToken< List< ; MyClass>>()
{
} .getType();

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

然而,我在fromJson一行中得到以下异常:

  java.lang.NullPointerException $ b $ 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)
在com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:56)
在com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
在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 )
在com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializa tionContextDefault.java:49)
在com.google.gson.Gson.fromJson(Gson.java:568)
在com.google.gson.Gson.fromJson(Gson.java:515)
在com.google.gson.Gson.fromJson(Gson.java:484)
在com.google.gson.Gson.fromJson(Gson.java:434)

I do catch JsonParseExceptions和result不为null。




  • 列表类型


    • args = ListOfTypes


      • list = null

      • resolvedTypes =类型[1]


      • loader = PathClassLoader

      • ownerType0 = null
      • ownerTypeRes = null

      • rawType = Class(java.util.ArrayList)
      • rawTypeName =java.util.ArrayList



    所以看起来getClass调用无法正常工作。任何建议...?



    编辑2:
    我检查了
    Gson用户指南。它提到了在将泛型类型解析为Json时应该发生的运行时异常。我做了错误(上面没有显示),就像在这个例子中一样,但根本没有得到这个例外。所以我按照用户指南的建议改变了序列化。

    Edit3:
    已解决,请参阅下面的答案。

    解决方案

    反序列化泛型集合的方法:

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

    ...

    类型listType = new TypeToken< ArrayList< YourClass>>(){}。getType();
    列表< YourClass> yourClassList = new Gson()。fromJson(jsonArray,listType);

    由于评论中的几个人提到了它,下面解释正在使用TypeToken 类。构造新的TypeToken< ...>(){} .getType()捕获一个编译时类型(< code>和> )添加到运行时 java.lang.reflect.Type 对象中。与 Class 对象不同,它只能表示原始(擦除)类型, Type 对象可以表示任何类型Java语言,包括泛型类型的参数化实例。
    $ b

    TypeToken 类本身没有public构造函数,因为你不应该直接构造它。相反,你总是构造一个匿名的子类(因此 {} ,这是表达式的必要部分)。



    由于类型擦除, TypeToken 类只能捕获编译时已知的类型。 (也就是说,对于类型参数 T,您不能执行新的TypeToken< List< T>>(){} .getType()



    有关更多信息,请参阅 TypeToken 类的文档


    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(){} 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!

    Edit:

    Now I have

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

    Edit2: I'v 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.

    Edit3: 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.

    这篇关于Google Gson - 反序列化列表&lt; class&gt;目的? (通用型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆