Search code examples
jsonjacksonjava-8guavajava-stream

Does Jackson support java 8 stream()?


I would like to use Jackson's Tree Model with Java 8 stream API, like so:

JsonNode jn = new ObjectMapper().readValue(src, JsonNode.class);
return jn.stream().anyMatch(myPredicate);

However, JsonNode does not seem to implement stream() and I could not find any standard helpers to do so.

JsonNode implements Iterable, so I can achieve the same results with Google Guava:

JsonNode jn = new ObjectMapper().readValue(src, JsonNode.class);
return Iterables.find(jn, myPredicate);

but what about pure Java solution?


Solution

  • JsonNode implements Iterable, so it has a spliterator(). You can use

    StreamSupport.stream(jn.spliterator(), false /* or whatever */);
    

    to get a Stream.