Jackson polymorphic deserialization with type property that is nested in object

Viewed 4753

I'm trying to find out a way to use the polymorphic deserialization feature of jackson in a way that it will deserialize my object based on a property that is nested in header/control object:

JSON 1 - CATEGORY1:

{
 "id":"someId",
 "header":{
           "category":"CATEGORY1",
           "somOtherProperty":"someValue"
          }
 "nextField":"nextValue",
 ...
}

JSON 2 - CATEGORY2

{
 "id":"someId",
 "header":{
           "category":"CATEGORY2",
           "somOtherProperty":"someValue"
          }
 "nextField":"nextValue",
 ...
}

Parent Class (annotations something like this)

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "category")
@JsonSubTypes({
        @Type(value = Category1Class.class, name = "CATEGORY1"), 
        @Type(value = Category2Class.class, name = "CATEGORY2") })
public class ParentClass{
    private Header header;
    private String nextField;
    ...
}

public class Header{
    private String category;
    private String somOtherProperty;
    ...
}

Child classes

@JsonTypeName("CATEGORY1")
public class Category1Class extends ParentClass{
    ...
}

@JsonTypeName("CATEGORY2")
public class Category2Class extends ParentClass{
    ...
}

Is there an out of the box functionality in jackson that would enable me to do this kind of deserialization or am I missing something?

1 Answers
Related