Skip to content

Add Optional<JsonNode> JsonNode.asOptional() convenience method #4867

@cowtowncoder

Description

@cowtowncoder

(note: part of https://github.com/FasterXML/jackson-future-ideas/wiki/JSTEP-3 changes)

Currently there is concept of "missing" node (impl MissingNode, detectable via JsonNode.isMissingNode()), returned in cases where Java 8 Optional.empty() would commonly be used. Partly this is due to "missing node" concept predating Jackson using Java 8 as base line, and partly due to wanting to avoid extra layering for common case of "not-missing" nodes.
Be that as it may, use of Optional is convenient with Java 8 Stream methods, so it makes sense to add some support.

To this end, let's add method

Optional<JsonNode> asOptional();

which will return

  • Optional.empty() for MissingNode
  • Optional.of(this) for all other JsonNode types
  • that is, Optional.ofNullable(this.isMissingNode() ? null : this) if implemented at JsonNode level

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.19Issues planned at 2.19 or later

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions