[英]Jackson polymorphic deserialization with nested type info property
I'm constrained by the given JSON structure: 我受限于给定的JSON结构:
{
"metadata": {
"eventName": "FooEvent",
"field1": "bla"
},
"event": { ... }
}
How can I deserialize it using polymorphic deserialization and nested type info property? 如何使用多态反序列化和嵌套类型信息属性对其进行反序列化? I'm using
metadata.eventName
nested property in @JsonTypeInfo
like this: 我在
@JsonTypeInfo
使用metadata.eventName
嵌套属性,如下所示:
@JsonTypeInfo(
use = Id.NAME,
include = As.EXISTING_PROPERTY,
visible = true,
property = "metadata.eventName"
)
@JsonSubTypes({
@Type(name="fooEvent", value = FooEvent.class)
@Type(name="barEvent", value = BarEvent.class)
})
public class EventPayload<T> {
private Metadata metadata;
private T event;
}
Given that config Jackson complains the property cannot be found: 鉴于配置杰克逊抱怨该物业无法找到:
com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'metadata.eventName' that is to contain type id (for class EventPayload)
at [Source: {
"metadata": {
"eventName": "FooEvent",
"field1": "bla"
},
"content": { ... }
}; line: 16, column: 1]
You are facing 2 issues here: 您在这里面临两个问题:
@JsonTypeInfo
and @JsonSubTypes
are meant for inheritance eg class FooEventPayload extends EventPayload
. @JsonTypeInfo
和@JsonSubTypes
用于继承,例如class FooEventPayload extends EventPayload
。 In your case EventPayload<T>
is a generic class and Jackson needs to be told what T
is with a TypeReference
Look here for instance EventPayload<T>
是一个泛型类,杰克逊需要告诉你什么是T
与TypeReference
在这里看看例如 Assuming you want a generic class I'd suggest serialize into a tree first, peek into the tree to get the property that specifies the type and then convert the tree to an object of this type. 假设你想要一个泛型类,我建议首先序列化到树中,查看树以获取指定类型的属性,然后将树转换为此类型的对象。 You can skip
@JsonTypeInfo
and @JsonSubTypes
. 您可以跳过
@JsonTypeInfo
和@JsonSubTypes
。 Eg 例如
// user object mapper to parse JSON into a tree (node is the root)
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(jsonString);
// use get as many times as needed by depth
// to get the value that defines the type to deserialise to
String type = node.get("metadata").get("eventName").textValue();
// convert JsonNode variable to the required type
if (type.equals("fooEvent")) {
EventPayload<FooEvent> event =
mapper.convertValue(node, new TypeReference<EventPayload<FooEvent>>(){});
} else if (type.equals("barEvent")) {
EventPayload<BarEvent> event =
mapper.convertValue(node, new TypeReference<EventPayload<BarEvent>>(){});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.