Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fschopp opened this issue Dec 3, 2013 · 2 comments
Milestone

Comments

@fschopp
Copy link

fschopp commented Dec 3, 2013

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."]]}
@cowtowncoder
Copy link
Member

One possible reason here is the precedence of @JsonSerialize annotations (property one has higher priority, masking one from class declaration) -- it is possible that "converter" from class is effectively ignored.

Thank you for unit test for this and other bug reports!

cowtowncoder added a commit that referenced this issue Dec 5, 2013
@cowtowncoder cowtowncoder added this to the 2.4.0 milestone Mar 18, 2014
@cowtowncoder
Copy link
Member

Turns out code just needed to re-check for existence of @JsonSerialize(using=...) after target type was detected. Will be included in 2.4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants