Skip to content

ObjectMapper.toJsonNode() Collector Stream Support #4691

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/tools/jackson/databind/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collector;

import tools.jackson.core.*;
import tools.jackson.core.exc.StreamReadException;
Expand Down Expand Up @@ -563,6 +564,31 @@ public Collection<JacksonModule> getRegisteredModules() {
return _savedBuilderState.modules();
}

/*
/**********************************************************************
/* Collectors for Stream support.
/**********************************************************************
*/

/**
* Creates a {@link Collector} that collects {@link JsonNode} elements into an {@link ArrayNode}.
* <p>
* This method uses this instance of {@link ObjectMapper} to create an empty {@link ArrayNode} and then adds each
* {@link JsonNode} to it.
* </p>
*
* @return a {@link Collector} that collects {@link JsonNode} elements into an {@link ArrayNode}
*
* @since 3.0
*/
public Collector<JsonNode, ArrayNode, ArrayNode> toJsonNode() {
return Collector.of(
this::createArrayNode, // supplier
ArrayNode::add, // accumulator
ArrayNode::addAll // combiner
);
}

/*
/**********************************************************************
/* Public API: constructing Parsers that are properly linked
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/tools/jackson/databind/ObjectMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Collectors;
import java.util.zip.ZipOutputStream;

Expand Down Expand Up @@ -100,6 +101,28 @@ public void testProps()
assertSame(nf, m.getNodeFactory());
}

@Test
public void testCollector()
{
final ObjectMapper objectMapper = new ObjectMapper();

final JsonNode jsonNodeResult = IntStream.range(0, 10)
.mapToObj(i -> {
ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put("testString", "example");
objectNode.put("testNumber", i);
objectNode.put("testBoolean", true);

return objectNode;
})
.collect(objectMapper.toJsonNode());

System.out.println(jsonNodeResult.toPrettyString());

assertEquals(10, jsonNodeResult.size());
jsonNodeResult.forEach(jsonNode -> assertFalse(jsonNode.isEmpty()));
}

// Test to ensure that we can check property ordering defaults...
@Test
public void testConfigForPropertySorting() throws Exception
Expand Down