Jackson Polymorphic Deserialization with Nested Information Type

I am limited to this JSON structure:

{
  "metadata": {
    "eventName": "FooEvent",
    "field1": "bla"
  },
  "event": { ... }
}

How can I deserialize it using polymorphic deserialization and a property of information of a nested type? I use a metadata.eventNamenested property in the @JsonTypeInfofollowing way:

@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 the Jackson configuration complains that the property could not 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]
+4
source share
1 answer

Here you are faced with two problems:

  • As you saw, Jackson cannot easily and simply comment on the use of a property in a JSON nested object to infer the type of deserialization in.
  • @JsonTypeInfo @JsonSubTypes , class FooEventPayload extends EventPayload. EventPayload<T> , , T TypeReference , ,

, , , , , , . @JsonTypeInfo @JsonSubTypes. .

// 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>>(){});
}
+1

Source: https://habr.com/ru/post/1692532/


All Articles