@Data
public class Parent {
private String type;
private List<ChildBase> children;
}
public abstract class ChildBase {
public void someMethod(){};
}
@Data
public class ChildA extends ChildBase {
private String bar;
}
@Data
public class ChildB extends ChildBase {
private String foo;
}
Then, the original JSON string is:
{
"type": "childA",
"children": [
{
"bar": "baaaar"
}
]
}
How can I deserialize this JSON string, the deserialized object(instance of Parent
), its children
field's actual type should be ChildA
, because the type
field specified;
In the question use-property-of-parent-object-to-determine-subclass-when-deserializing, the answer blow shows the way to determine subclass, by it doesn't work with a list field.