Reputation: 1668
I deserialize jsons like this:
{
"type":"a",
"payload" : {...}
}
where payload type depends on type. My class:
public class Sth<T extends Payload> {
@JsonProperty("type")
private String type;
@Valid
private T payload;
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "type",
visible = true,
defaultImpl = NoClass.class)
@JsonSubTypes({
@JsonSubTypes.Type(value = APayload.class, name = "a"),
@JsonSubTypes.Type(value = BPayload.class, name = "b"),
@JsonSubTypes.Type(value = CPayload.class, name = "c")})
public void setPayload(T payload) {
this.payload = payload;
}
public void setType(String type) {
this.type = type;
}
}
I have also type "d" with no payload. If I try to deserialize:
{
"type":"d",
"payload" : null
}
it works but it doesn't work with no payload:
{
"type":"d",
}
How to make it work with last example?
Stacktrace of error that I get:
[error] Caused by: com.fasterxml.jackson.databind.JsonMappingException: Missing property 'payload' for external type id 'type
[error] at [Source: N/A; line: -1, column: -1]
[error] at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
[error] at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:700)
[error] at com.fasterxml.jackson.databind.deser.impl.ExternalTypeHandler.complete(ExternalTypeHandler.java:160)
[error] at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeWithExternalTypeId(BeanDeserializer.java:690)
[error] at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeWithExternalTypeId(BeanDeserializer.java:639)
[error] at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:266)
[error] at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:124)
[error] at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:2965)
[error] at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1587)
[error] at com.fasterxml.jackson.databind.ObjectMapper.treeToValue(ObjectMapper.java:1931)
[error] at play.libs.Json.fromJson(Json.java:47)
Upvotes: 22
Views: 15368
Reputation: 151
There is explicit deserialization feature for this:
new ObjectMapper().configure(
DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY,
false);
Upvotes: 15
Reputation: 97331
I've also encountered this issue, and could not find an elegant solution using the mechanisms provided by Jackson (custom BeanDeserializer
, BeanDeserializerModifier
, etc.).
It looks like a bug in the way external type IDs are handled. I worked around it by:
JsonNode
;null
node if the required property is not present;JsonNode
to my desired value type.My code looks something like the following:
public <T> T decode(String json, Class<T> type) throws IOException {
JsonNode jsonNode = mapper.readTree(json);
if (jsonNode.isObject() && (jsonNode.get("payload") == null || jsonNode.get("payload").size() == 0)) {
ObjectNode objectNode = (ObjectNode) jsonNode;
objectNode.putNull("payload");
}
return mapper.treeToValue(jsonNode, type);
}
Upvotes: 3
Reputation: 1067
I believe I have encountered the same issue you documented.
I was receiving the following exception
com.fasterxml.jackson.databind.JsonMappingException: Missing property 'content'
for external type id 'contentType
I ended up getting it working by adding the @JsonTypeId annotation to the type field.
public interface Content {
}
public class Post implements Content {
public String content = "bar";
}
public class Foo {
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.EXTERNAL_PROPERTY,
property = "contentType")
@JsonSubTypes({
@JsonSubTypes.Type(value = Post.class, name = "post")
})
public Content content;
@JsonTypeId
public ContentType contentType;
public enum ContentType {
"post"
}
}
Upvotes: 1