My Json can be:
{
"bird": "Owl",
"Animal": {
"type": "Dog",
"breed": "mutt"
}
}
Or
{
"bird": "Owl",
"Animal": {
"type": "Cat",
"color": "white"
}
}
Class for Json,
Class ABC {
public String bird;
Animal animal;
}
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY,property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value=Dog.class,name="dog"),
@JsonSubTypes.Type(value=Cat.class,name="cat") }
)
Class Animal {
public String type;
}
Class Dog extends Animal {
public String breed;
}
Class Cat extends Animal {
public String color;
}
when i read json,
ObjectMapper solver = new ObjectMapper();
Objectabc = solver.readValue(new File(filename), ABC.class);
I am able to access bird
,but
Objectabc.animal.type
prints null
.
Please tell me how can i access type
and breed
or color
.