-
-
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 Stream-friendly alternative to JsoneNode.fields()
: Set<Map.Entry<String, JsonNode>> properties()
#3809
Comments
/cc @yawkat @pjfanning -- this seems straight-forward and I hope to do PR soon (maybe tonight). But wanted to see if you had suggestions, opinions. This is based on realizing how awkward use of Iterator-based alternatives currently is for |
having a stream is nice but i think in most cases an Iterable would be preferable so that you can use a foreach loop. So basically an Iterable that behaves like fields(). |
I tend to avoid using streams in hot code paths because performance leaves a bit to be desired and is harder to reason about. I wonder if there's an intermediate option in which for (JsonNode fieldValue : node.values()) { as well as streams: node.values()
.stream()
./*etc*/ |
Yeah perhaps exposing Since So perhaps I should only add |
ObjectNode.fields()
ObjectNode.fields()
(ObjectNode.properties()
returning Iterable<String, JsonNode>
?)
The existing impl is:
what's wrong with this? |
@pjfanning It is cumbersome to go from Not the biggest thing in the world but one of ergonomic pet peeves. |
going from Iterable to stream is also annoying. You can go through the spliterator but it's not easy. I think that actually, returning a collection like @carterkozak suggested would be fine. If you look at AbstractCollection, it actually only requires two methods for the most basic implementation, We don't have to implement the mutation methods, though eg remove will work by default if we just use So a Collection backed by an unmodifiable iterator would be no additional api exposure, it would allow easy enhanced for (like Iterable), and it has a stream method (Iterable does not). |
Ah. So My initial concern return So I think the simplest and (no less) safe (than before) way to go is to just add:
and be done with it? No need to construct |
I like immutable myself but Java users can't be weaned off mutable side effect laden coding. Any chance that wrapping the collection as immutable could be considered? |
I would bias toward immutability, this opens the possibility of deprecating and eventually deleting the mutable iterator methods. It's always easier to make an immutable result mutable later on. It's possible, however unlikely, that wrapping the collection allocates enough wrappers that performance is impacted, but I suspect it won't be measurable, and it's not painful to reverse course. |
My point is this tho: while we could wrap Or maybe formulate this different way: given the situation as of Jackson 2.14, ability to change entries exists, perhaps as a feature, so keeping things consistent, Collection/Set of We could definitely change this for Jackson 3.0, but even there the question is... what is the main goal? To design actually immutable JsonNode-like API one has to start from ground up (I have thought about this over past 10 years since I, too, like Immutability a lot -- and would love to have Immutable Tree Model). |
Possible, but as per my comments above, what would be the benefit? (keeping in mind that |
ObjectNode.fields()
(ObjectNode.properties()
returning Iterable<String, JsonNode>
?)ObjectNode.fields()
: Set<Map.Entry<String, JsonNode>> properties()
ObjectNode.fields()
: Set<Map.Entry<String, JsonNode>> properties()
JsoneNode.fields()
: Set<Map.Entry<String, JsonNode>> properties()
Currently with Jackson 2.x the only way to traverse entries of
ObjectNode
is usingfields()
method.This has the problem that it returns
Iterator
(ofMap.Entry<String, JsonNode>
) which is awkward to use (with Java 8 and above).Similarly we can consider adding methods to replace:
for "values" stream.
Naming-wise I am thinking of
JsonNode.entryStream()
-- but could also considerentries()
orproperties()
?JsonNode.valueStream()
-- but could consider justvalues()
?EDIT: At this point, favoring
JsonNode.values()
for single-value Stream (Array elements, Map entry values)JsonNode.properties()
forMap.Entry<String, JsonNode>
valued streambacked by simple implementations of:
ArrayList.stream()
(ArrayNode),Map.values().stream()
(ObjectNode)Map.entrySet().stream()
The text was updated successfully, but these errors were encountered: