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

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

Closed
fschopp opened this issue Dec 2, 2013 · 6 comments

Comments

@fschopp
Copy link

fschopp commented Dec 2, 2013

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."]]}
@fschopp
Copy link
Author

fschopp commented Dec 3, 2013

Looks like the IterableSerializer is somewhat out of date compared to the CollectionSerializer?

@cowtowncoder
Copy link
Member

Ok I'll have a look. It is possible that IterableSerializer could be missing something other serializers have.

@cowtowncoder
Copy link
Member

One quick question: is this with Java 8? I recall some oddities with java.util.Iterator on Java 8, but not enough time to dig in (I am not using 8 for anything other than building new Java 8 date/time module).

@cowtowncoder
Copy link
Member

No, I can reproduce this with 1.7 as well. Different issue.

@cowtowncoder
Copy link
Member

Looks like the problem is that annotation for serializer to use, for content type, gets dropped if there is as annotation property. Commenting out that property removes the problem. So need to see where type is being overridden as that is where associate handler is probably dropped.

@cowtowncoder
Copy link
Member

Ah. It wasn't anything fancy -- as you correctly suggested, IterableSerializer was to blame; it was ignoring element serializer that was correctly passed & set, and tried to fetch serializer dynamically.
So at least one out of 3 is resolved.

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