简体   繁体   中英

Jackson custom deserialiser delegate back to default one

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

ps I do need custom deserialiser and cannot use type annotations.

You can use following code to achieve this.

JsonParser parser = nodeToDelegate.traverse();
parser.setCodec(jp.getCodec());
parser.readValueAs(<Type>.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM