简体   繁体   English

如何使用Jackson JSON和JAVA反序列化多态LinkedList?

[英]How to deserialize polymorphic LinkedList with Jackson JSON and JAVA?

I´m having some problems when i try to deserializing a Java class. 尝试反序列化Java类时遇到一些问题。 My classes are : 我的课程是:

Abstract Class A {
Long ID
String name 
...
}
Class B extends A{ 
String Type
String Value
}
Class C extends A{
List<A> typedList;
}

And my Serialization/Deseralization Methods: 和我的序列化/反序列化方法:

public String serialize(T object) {
        String returnValue = "";

        try {
            returnValue = mapper.writeValueAsString(object);

        } catch (JsonGenerationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println(returnValue);
        return returnValue;
    }

public List<T> unserialize(String data, Class<?> clazz) {
        List<T> returnValue = new LinkedList<T>();
        //JavaType topMost = mapper.getTypeFactory().constructParametricType(MyWrapper.class, ActualClassRuntime.class); 
        try {

            returnValue = (List<T>) mapper.readValue(data,clazz);
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return returnValue;
    }

The list inside the C class contain B and C elements. C类内部的列表包含B和C元素。 Serialization is done without problems but when i tried to deserialize C , Jackson can´t rebuild my typedList correctly. 序列化没有问题,但是当我尝试反序列化C时,Jackson无法正确重建我的typedList。 It uses LinkedHashMap instead of LinkedList and don´t use the correct types. 它使用LinkedHashMap代替LinkedList,并且不使用正确的类型。 Any ideias how to solve this ? 任何想法如何解决这个问题? Thanks in advance! 提前致谢!

If you expect to get a List, you must pass a List type: here you are giving type of clazz but expect type of List<T> . 如果希望获得列表,则必须传递一个List类型:在这里,您给出的是clazz类型,但期望为List<T>类型。 This won't work, even if clazz is of type List . 即使clazzList类型,这也不起作用。 So you need to pass proper type. 因此,您需要传递正确的类型。 But for List (and Map) types, you further need to specify contents type: and type variable does not do. 但是对于列表(和地图)类型,您还需要指定内容类型:而类型变量则不行。 What works is something like: 有效的是这样的:

List<MyType> list = mapper.readValue(jsonSource, new TypeReference<List<MyType>>() { });

where TypeReference must be used due to Java type erasure. 由于要清除Java类型, TypeReference必须使用TypeReference

You can also construct structured types from components dynamically, if they are passed as arguments: 如果将它们作为参数传递,则还可以从组件动态构造结构化类型:

Class<?> elem = ...;
List<?> list = mapper.readValue(jsonSource,
  mapper.getTypeFactory().constructCollectionType(ArrayList.class, elem);

The reason you get LinkedHashMap is just because of missing type information: if JSON Object is encountered, and requested type is java.lang.Object (as would be the case if you just passed type List.class to bind to), this is the type that would be used. 获得LinkedHashMap的原因仅是因为缺少类型信息:如果遇到JSON对象,并且请求的类型为java.lang.Object (如果您只是通过传递要绑定的类型List.class就是这种情况)。将使用的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM