-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 JsonNode.optional(String name)
and JsonNode.optional(int index)
to be able to get an Optional JsonNode
#2145
Comments
You may not be familiar with
in cases where there is no matching node. You can call usual methods on So. Although I am not totally opposed to some level of compatibility with JDK Optional for Jackson 3.0 (2.x it can not be done as that's Java 8 feature), I want to make sure there is real need. |
I am already aware of the missingnode technique, I just feel that my optional-variant is missing. |
Parallel to that, what about a method In that case, we would lose the option of using the |
I don't think I want But perhaps adding simple |
JsonNode.optional(String name)
and JsonNode.optional(int index)
to be able to get an Optional JsonNode
Lemme start this one if no one has started? Wrt implementation details, would be one PR seems okay? Including following tasks
|
Sounds good. The only (?) open question -- which I should have noted here -- is the naming.
WDYT @JooHyukKim, @pjfanning, @yawkat ? |
Hmmm I was actually 50/50 on 'getOptional()' vs 'optional()'. Atm I am more concerned about users complaining about lack of intuitiveness ("what does opt mean?") rather than method name being too long. But I'm open to other suggestions as well. But with the current PR I chose to follow the title. |
Makes sense. Let's see what others think. Also creating some more issues for simple work:
|
it should be put into consideration that a method name |
@burdoto Good point, traditional get-ter names are too easily exposed literally anything... |
Also I just realized this change is going into 2.19 as well... meaning coming up with best name is quite important. |
Ok yes, I think going with |
NOTE: considered part of JSTEP-3
So I had this idea of how nice it would be to have a getter method that returns an optional JsonNode, like
#path(String name)
does, but when returning an Optional, it would be simpler to only use a node when its actually present, or handle cases that its not present in.Current Solution:
With optional nodes:
In this scenario, the difference is counter intuitive, because I can use a ternary-if in this case.
See this scenario:
Without the addition:
With the addition:
I think this scenario could show why the addition could be helpful.
If this idea gets approved and it is wished for; I could work on a PR myself.
The text was updated successfully, but these errors were encountered: