问题
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:
- Create new List implementation
- Write serializer for new type
- 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