Custom deserialization on top of reflective deserialization with Jackson

jackson, java, json

Solution

Polymorphic deserialization should be able to handle deriving the type of the input from the content of the input. If that's what you need the custom deserialization for, you can achieve it via annotations. See this example for reference.

(Apologies for not answering the actual question; this answers what I'm inferring to the "root" question, from the poster's comment).

Problem

I want to run some custom code when deserializing a particular type with Jackson 1.9. However, I don't want to hand-write the whole deserializer, just add to the default behaviour. Trying to do this in a `JsonDeserializer` the naive way results in infinite recursion, however. Currently, my approach uses a completely separate `ObjectMapper`, but that feels like a huge hack. ``` private static class SpecialDeserializer extends JsonDeserializer<Special> { @Override public Special deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode jsonNode = jp.readValueAsTree(); ObjectMapper otherMapper = getOtherMapper(); Special special = otherMapper.readValue(jsonNode, Special.class); special.setJsonNode(jsonNode); return special; } } ```

Original source