I'm constrained by the given JSON structure:
{
"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(
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 e.g 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
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
. E.g.
// 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>>(){});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With