1

In a custom Jackson deserialiser, is there a way to delegate certain properties back to the default deserialiser?

@Override
public final T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectNode node = jp.getCodec().readTree(jp);

    T type = createType();
    //custom deserialise some fields here
    ...

    // Is there a way to delegate everything else back to Jackson?

    ObjectNode nodeToDelegate = node.get("someField");
    // delegate back to jackson and deserialise into `type`
    // nodeToDelegate can be anything - Number / Object / Array / etc.
}

p.s. I do need custom deserialiser and cannot use type annotations.

SDekov
  • 9,276
  • 1
  • 20
  • 50

1 Answers1

1

You can use following code to achieve this.

JsonParser parser = nodeToDelegate.traverse();
parser.setCodec(jp.getCodec());
parser.readValueAs(<Type>.class);
S.K.
  • 3,597
  • 2
  • 16
  • 31
  • The problem with this is, one has first to figure out the type of the property and then once you have the value, figure out which setter to call / field to set - imagine private fields, renamed setters, etc (which is what Jackson is good at) – SDekov Aug 20 '18 at 09:20