jacksonjava-8java-streamfasterxml

Create Java 8 Stream from ArrayNode


Is it possible to create stream from com.fasterxml.jackson.databind.node.ArrayNode?
I tried:

ArrayNode files = (ArrayNode) json.get("files");
Stream<JsonNode> stream = Stream.of(files);

But it will actually give stream of one element, the initial ArrayNode object.
Correct result should be Stream<JsonNode>, can I achieve it?


Solution

  • ArrayNode implements Iterable. Iterable has a spliterator() method. You can create a sequential Stream from a Spliterator using

    ArrayNode arrayNode = (ArrayNode) json.get("xyz");
    StreamSupport.stream(arrayNode.spliterator(), false)