Error serializing Typed collection with Jackson

 ̄綄美尐妖づ 提交于 2019-12-22 10:01:18

问题


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"}
    }

}

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


回答1:


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

  1. Create new List implementation
  2. Write serializer for new type
  3. Use this type in your POJO classes

New Java class which extends ArrayList:

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

    private static final long serialVersionUID = 1L;
}

Serializer for above class:

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()));
}

Above example prints:

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


来源:https://stackoverflow.com/questions/22103451/error-serializing-typed-collection-with-jackson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!