-
-
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
Allow for-each loops with object nodes #1590
Conversation
This change allows using the much more elegant for-each constructs: ``` for(String field : objectNode.fieldNamesSet()) ``` and ``` for(Map.Entry<String, JsonNode> enry : objectNode.fieldSet()) ``` in addition to the current iterator-based access, which is more code intensive: ``` Iterator<String> fieldNameIterator = objectNode.fieldNames(); while(fieldNameIterator.hasNext()) { String fieldName = fieldNameIterator.next(); } ``` and ``` Iterator<Map.Entry<String, JsonNode>> entryIterator = objectNode.fields(); while(entryIterator.hasNext()) { Map.Entry<String, JsonNode> entry = entryIterator.next(); } ```
Add fieldNamesSet() and fieldsSet() top JsonNode.
* @return Set that can be used to traverse all key/value pairs for | ||
* object nodes; empty set (no contents) for other types | ||
*/ | ||
public Set<Map.Entry<String, JsonNode>> fields() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be fieldSet
, as there is existing fields
method (that returns Iterator
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, yes. This is supposed to be fieldSet
.
Yes, I concur in all points. Addition of The only change is, I think, that due to backwards compatibility concerns, method probably should not be added in Actually come to think of it; instead of |
I chose For 3.0, I would suggest to kick the |
Yes, agreed, for 3.x can and should do bigger clean up. I'll tag this as 3.x change. |
I think this is mostly obsoleted by #3809 (which adds Closing this PR. |
This change allows using the much more elegant for-each constructs:
and
in addition to the current iterator-based access, which is more code intensive:
and
IMPORTANT: This also needs an addition to TreeNode in jackson-core, to add this interface method:
or the
@Override
needs to be removed from this method inJsonNode
.In my opinion (you may want to consider this for version 3?), the
Iterator
based methods should be replaced withIterable
methods completely. I.e. if API breaking changes are acceptable, I would makefields()
return anIterable<Map.Entry<String, JsonNode>>
rather than an iterator, as.fieldNames().iterator()
is exactly equivalent; andfieldNames()
returnIterable<String>
rather than an iterator for the same reason. Java allows foreach loops forIterable
, but not forIterator
.