使用Jackson序列化类型集合时出错 [英] Error serializing Typed collection with Jackson

查看:151
本文介绍了使用Jackson序列化类型集合时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用混音序列化一个集合,但杰克逊不会保存类型信息。这是一个基本测试,说明了发生的情况:

I'm trying to serialize a collection using mixings, but Jackson won't save the type info. This is a basic test illustrating what happens:

public class CollectionSerializationTest {

    interface Common extends Serializable {

    }

    class A implements Common {
        private static final long serialVersionUID = 1L;
    }

    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
    @JsonSubTypes({ @JsonSubTypes.Type(value = A.class, name = "CODE") })
    class AMixIn {

    }

    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
    @JsonSubTypes({ @JsonSubTypes.Type(value = B.class, name = "CODE") })
    class BMixIn {

    }

    class B implements Common {
        private static final long serialVersionUID = 1L;
    }

    @Test
    public void test() throws JsonGenerationException, JsonMappingException,
            IOException {
        ObjectMapper om = new ObjectMapper();
        List<Common> list = new ArrayList<Common>();
        A a = new A();
        B b = new B();
        list.add(a);
        list.add(b);
        om.getSerializationConfig().addMixInAnnotations(A.class, AMixIn.class);
        om.getSerializationConfig().addMixInAnnotations(B.class, BMixIn.class);
        System.out.println(om.writeValueAsString(list)); // Outputs [{},{}]
        System.out.println(om.writeValueAsString(a));// Outputs {"type":"CODE"}
    }

}

如何实现输出 [{type :CODE},{type:CODE}] 第一次输出?

How do I achieve the output [{"type":"CODE"},{"type":"CODE"}] on the first output?

推荐答案

我不确定这是否是最简单的解决方案,但我认为您可以这样做:

I am not sure whether it is simplest solution, but I think that you can do it in this way:


  1. 创建新列表实现

  2. 为新类型编写序列化程序

  3. 在POJO类中使用此类型

新的Java类扩展 ArrayList

@JsonSerialize(using = JacksonListSerializer.class)
class JacksonList<E> extends ArrayList<E> {

    private static final long serialVersionUID = 1L;
}

上述类的序列化器:

class JacksonListSerializer extends JsonSerializer<JacksonList<?>> {

    @Override
    public void serialize(JacksonList<?> list, JsonGenerator generator, SerializerProvider provider)
            throws IOException, JsonProcessingException {
        generator.writeStartArray();
        if (list != null) {
            for (Object item : list) {
                generator.writeObject(item);
            }
        }
        generator.writeEndArray();
    }
}

现在,您可以在示例中使用此列表:

Now, you can use this list in your example:

public static void main(String[] args) throws IOException {
    ObjectMapper om = new ObjectMapper();
    List<Common> list = new JacksonList<Common>();
    list.add(new A());
    list.add(new B());
    om.addMixInAnnotations(A.class, AMixIn.class);
    om.addMixInAnnotations(B.class, BMixIn.class);
    System.out.println(om.writeValueAsString(list));
    System.out.println(om.writeValueAsString(new A()));
}

以上示例打印:

[{"type":"CODE"},{"type":"CODE"}]
{"type":"CODE"}

这篇关于使用Jackson序列化类型集合时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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