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;
}
}