I am using Jackson to parse JSON that I have no control over. The JSON looks like this:
{
"status":"0"
"type":"type1"
"info": {
// additional fields
}
}
My class looks like this
public class Response {
private String status;
private String type;
private Info info
}
The subclass of Info that I use depends on the type property, so my mapping for info is
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
@JsonSubTypes(value = {
@JsonSubTypes.Type(value = Type1Info.class, name = "type1"),
@JsonSubTypes.Type(value = Type2Info.class, name = "type2") })
public abstract class Info {
// some fields
}
As far as I can tell this is the correct way to use type info when the distinguishing element is at the same level as the element that has to be casted. But this doesn't work, I always get the same error:
com.fasterxml.jackson.databind.JsonMappingException: Unexpected token
(END_OBJECT), expected FIELD_NAME: missing property 'type' that is to
contain type id
If I change EXTERNAL_PROPERTY to PROPERTY I still get the same error. Is my understanding of EXTERNAL_PROPERTY wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…