--
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 jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I think this is what SimpleModule.registerSubtypes(NamedType...) lets you do.-+ Tatu +-
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "messageName")
public abstract static class NamedBase
{
public float basef;
public NamedBase() {
basef = 4.5f;
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("basef", basef).toString();
}
}
public static class SubName1 extends NamedBase
{
public int a1;
public SubName1() {
a1 = 45;
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString())
.append("a1", a1)
.toString();
}
}
public static class SubName2 extends NamedBase
{
public String b2;
public SubName2() {
b2 = "45";
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString())
.append("b2", b2)
.toString();
}
}
public static class TwoNamedBases
{
private NamedBase x1, x2;
@JsonCreator
public TwoNamedBases(@JsonProperty("x1") NamedBase x1, @JsonProperty("x2") NamedBase x2) {
this.x1 = x1;
this.x2 = x2;
}
public NamedBase getX1() {
return x1;
}
public NamedBase getX2() {
return x2;
}
@Override
public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("x1", x1)
.append("x2", x2)
.toString();
}
}
@Test
public void serialize_deserialize_named_classes() throws IOException {
NamedBase sut1 = new SubName1();
NamedBase sut2 = new SubName2();
TwoNamedBases sut = new TwoNamedBases(sut1, sut2);
SimpleModule subtypes = new SimpleModule().registerSubtypes(new NamedType(SubName1.class, "sub1"),
new NamedType(SubName2.class, "sub2"));
ObjectMapper mapper = new ObjectMapper().registerModule(subtypes);
String actual1 = mapper.writeValueAsString(sut);
assertThat(actual1).isEqualTo(
"{\"x1\":{\"messageName\":\"sub1\",\"basef\":4.5,\"a1\":45},\"x2\":{\"messageName\":\"sub2\",\"basef\":4.5,\"b2\":\"45\"}}");
TwoNamedBases actual2 = mapper.readValue(actual1, TwoNamedBases.class);
assertThat(actual2.toString()).isEqualTo(sut.toString());
}
--
Correct, Wiki has not been significantly updated. This is one area where contributions would be very welcome.Note, too, that Fasterxml wiki is planned to be deprecated and replaced either by Jackson github project wikis & READMEs, or perhaps by revised Fasterxml web site. Most up-to-date docs are within github project READMEs, wikis.In fact, fasterxml wiki is mostly for 1.x features.-+ Tatu +-
--