Closed
Description
Jackson version: 2.3.0
In the example below, a list of C
s should be serialized by converting each C
to an A
. Unfortunately, Jackson uses the BeanSerializer for the conversion results instead of the serializer configured for A
.
@JsonSerialize(using = ASerializer.class)
static class A {
public String unexpected = "Bye.";
}
static class B {
@JsonSerialize(as = List.class, contentAs = C.class)
public List<C> list = Arrays.asList(new C());
}
@JsonSerialize(converter = CtoAConverter.class)
static class C { }
static class CtoAConverter extends StdConverter<C, A> {
@Override
public A convert(C value) {
return new A();
}
}
static class ASerializer extends JsonSerializer<A> {
@Override
public void serialize(A a, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
jsonGenerator.writeStartArray();
jsonGenerator.writeString("Hello world.");
jsonGenerator.writeEndArray();
}
}
@Test
void testBSerialization() throws Exception {
System.out.println(new ObjectMapper().writeValueAsString(new B()));
}
Actual output:
{"list":[{"unexpected":"Bye."}]}
Expected output:
{"list":[["Hello world."]]}
Metadata
Metadata
Assignees
Labels
No labels