Skip to content

Converted object serialized with BeanSerializer instead of the configured serializer #359

Closed
@fschopp

Description

@fschopp

Jackson version: 2.3.0

In the example below, a list of Cs 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions