Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cowtowncoder opened this issue Dec 27, 2024 · 0 comments · Fixed by #4872
Closed

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

cowtowncoder opened this issue Dec 27, 2024 · 0 comments · Fixed by #4872
Labels
2.19 Issues planned at 2.19 or later
Milestone

Comments

@cowtowncoder
Copy link
Member

cowtowncoder commented Dec 27, 2024

(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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.19 Issues planned at 2.19 or later
Projects
None yet
1 participant