Search code examples
javajsonjacksonmixins

Error serializing Typed collection with 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"}
    }

}

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


Solution

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