Search code examples
javajsonserializationjacksonjsonserializer

JSONTypeInfo to not ignore property in inheritance mapping


I am using following dependency for JSON serialization/deserialization

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.4</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.7.4</version>
</dependency>

I have inheritance mapping.

Following is Parent class.

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "isOne")
@JsonSubTypes({ @Type(value = One.class, name = "true"), @Type(value = Two.class, name = "false") })
public class Parent extends AbstractValueObject {
     private Boolean isOne;
}

And we have two sub class One and Two extended by Parent.

This mapping is working when I serialize/deserialize with the help of property isOne.

But the problem is when JSON is converted to class jackson removes the property isOne. Is there any way which does not remove that property. It don't recommend any dummy property as long as i can have actual.


Solution

  • According to the doc for JsonTypeInfo you can use visible=true

    Try this:

    @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "isOne", visible = true)