Skip to content

Jackson does not honor contentUsing in @JsonSerialize under some circumstances #358

@fschopp

Description

@fschopp

Jackson version: 2.3.0

While trying to work around Issue #357, I found another bug.

Jackson does not honor contentUsing in @JsonSerialize under some circumstances.

static class A {
    public String unexpected = "Bye.";
}

static class B {
    @JsonSerialize(as = Iterable.class, contentUsing = ASerializer.class)
    public List<A> list = Arrays.asList(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()));
}

This gives the following output:

{"list":[{"unexpected":"Bye."}]}

However, replacing as = Iterable.class by as = List.class gives output:

{"list":[["Hello world."]]}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions