Hello,
I was hoping for some feedback on the following problem involving Type
Resolvers during serialization of classes that use @JsonValue. The example
I will give will use the upcoming Ion data-format
<https://github.com/FasterXML/jackson-dataformat-ion/tree/master/src/test/java/com/fasterxml/jackson/dataformat/ion>.
Specifically, using Ion type annotations
<https://amzn.github.io/ion-docs/spec.html#annot> to resolve types via the
IonAnnotationTypeResolverBuilder
<https://github.com/FasterXML/jackson-dataformat-ion/blob/master/src/main/java/com/fasterxml/jackson/dataformat/ion/polymorphism/IonAnnotationTypeResolverBuilder.java>
much
like the PolymorphicRoundTripTest
<https://github.com/FasterXML/jackson-dataformat-ion/blob/master/src/test/java/com/fasterxml/jackson/dataformat/ion/PolymorphicRoundtripTest.java>
.
Given a mapper configured with a module much like in the linked test:
IonObjectMapper mapper = new IonObjectMapper();
mapper.registerModule(new IonAnnotationModule());
And the following classes:
@JsonTypeResolver(IonAnnotationTypeResolverBuilder.class)
public static class ClassA {}
public static class SubClassB extends ClassA {
public final String value;
@JsonCreator
public SubClassB(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
@Override
public String toString() {
return "SubClassB[value=" + value + "]";
}
}
I can successfully deserialize to the correct type:
ClassA a =
mapper.readValue("'some.test.package.SubClassB'::\"some_value\"",
ClassA.class);
System.out.println(a); //Output: SubClassB[value=some_value]
But cannot seem to include type information when serialization:
System.out.println(mapper.writeValueAsString(a)); //Output: "some_value"
The desired output would be: 'some.test.package.SubClassB'::"some_value".
Is this possible? I see a potential option in the JsonValueSerializer
class
<https://github.com/FasterXML/jackson-databind/blob/master/src/main/java/com/fasterxml/jackson/databind/ser/std/JsonValueSerializer.java#L51-L57>but
unsure if/how this would be used to solve the problem.
Thank you,
Michael
--
You received this message because you are subscribed to the Google Groups
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.