Skip to content

Add DeserializationContext.readTreeAsValue() methods for more convenient conversions for deserializers to use #3002

@cowtowncoder

Description

@cowtowncoder

(note: inspired by #3000)

A very common use pattern for deserializers is to read content as a tree (JsonNode) and then extract properties, either simple scalar values (Strings, numbers, boolean), or to further bind as POJOs via delegation.
But while there is a way to achieve that, the "proper" way is somewhat complicated, by:

  1. Constructing JsonParser out of JsonNode (method traverse())
  2. Either locating deserializer to use (find[Non]ContextualValueDeserializer()) and calling it; or using short-cut of readValue() (or readPropertyValue()`)

Because of this, many users instead use construct like:

JsonNode tree = ctxt.readTree(p);
MyPojo value = ctxt.getCodec(). treeToValue(tree.get("pojo"), MyPojo.class);

which has been available since Jackson 2.0, but has some issue regarding not retaining current deserialization context and things like Attributes set.

So it would make sense to add a convenience method that does what is intended starting with existing JsonNode (first checking for null and "missing node"?), locating deserializer & reading.
Naming TBD, could be:

DeserializationContext.treeToValue(JsonNode, Class<?> targetType); // and other method with `JavaType`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions