7

I have a use case very similar to Jackson deserializing nested polymorphic type , except that I want to have two nested abstract types deserialized based on JsonTypeInfo.As.PROPERTY with different property names.

Supertype:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "first_level_discriminant")
@JsonSubTypes({
        @JsonSubTypes.Type(value = SecondLevel.class, name = "second_level")
})
public abstract class FirstLevel { }

Second-level type:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "second_level_discriminant")
@JsonSubTypes({
        @JsonSubTypes.Type(value = ConcreteType.class, name = "concrete_type")
})
public abstract class SecondLevel { }

Concrete type:

public class ConcreteType extends SecondLevel {
    public String value;
}

But when I try to deserialize {"first_level_discriminant":"second_level","second_level_discriminant":"concrete_type","value":"value"}, I get a regular abstract type error (Can not construct instance of SecondLevel: abstract types either need...). Deserializing SecondLevel directly works, and so does deserializing concrete subtypes of FirstLevel. There is a "checked" answer on the mentioned question, but as far as I understand, it suggests flattening the class hierarchy to single polymorphic type.

fghkngfdx
  • 135
  • 4

0 Answers0