Skip to content

Commit 4fcaf6f

Browse files
committed
ObjectMapper.toJsonNode base implementation
unit-test in ObjectMapperTest to validate base solution
1 parent 16de009 commit 4fcaf6f

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/main/java/tools/jackson/databind/ObjectMapper.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Map;
1111
import java.util.concurrent.ConcurrentHashMap;
1212
import java.util.concurrent.atomic.AtomicReference;
13+
import java.util.stream.Collector;
1314

1415
import tools.jackson.core.*;
1516
import tools.jackson.core.exc.StreamReadException;
@@ -563,6 +564,31 @@ public Collection<JacksonModule> getRegisteredModules() {
563564
return _savedBuilderState.modules();
564565
}
565566

567+
/*
568+
/**********************************************************************
569+
/* Collectors for Stream support.
570+
/**********************************************************************
571+
*/
572+
573+
/**
574+
* Creates a {@link Collector} that collects {@link JsonNode} elements into an {@link ArrayNode}.
575+
* <p>
576+
* This method uses this instance of {@link ObjectMapper} to create an empty {@link ArrayNode} and then adds each
577+
* {@link JsonNode} to it.
578+
* </p>
579+
*
580+
* @return a {@link Collector} that collects {@link JsonNode} elements into an {@link ArrayNode}
581+
*
582+
* @since 3.0
583+
*/
584+
public Collector<JsonNode, ArrayNode, ArrayNode> toJsonNode() {
585+
return Collector.of(
586+
this::createArrayNode, // supplier
587+
ArrayNode::add, // accumulator
588+
ArrayNode::addAll // combiner
589+
);
590+
}
591+
566592
/*
567593
/**********************************************************************
568594
/* Public API: constructing Parsers that are properly linked

src/test/java/tools/jackson/databind/ObjectMapperTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.nio.charset.StandardCharsets;
88
import java.nio.file.*;
99
import java.util.*;
10+
import java.util.stream.IntStream;
1011
import java.util.stream.Collectors;
1112
import java.util.zip.ZipOutputStream;
1213

@@ -100,6 +101,28 @@ public void testProps()
100101
assertSame(nf, m.getNodeFactory());
101102
}
102103

104+
@Test
105+
public void testCollector()
106+
{
107+
final ObjectMapper objectMapper = new ObjectMapper();
108+
109+
final JsonNode jsonNodeResult = IntStream.range(0, 10)
110+
.mapToObj(i -> {
111+
ObjectNode objectNode = objectMapper.createObjectNode();
112+
objectNode.put("testString", "example");
113+
objectNode.put("testNumber", i);
114+
objectNode.put("testBoolean", true);
115+
116+
return objectNode;
117+
})
118+
.collect(objectMapper.toJsonNode());
119+
120+
System.out.println(jsonNodeResult.toPrettyString());
121+
122+
assertEquals(10, jsonNodeResult.size());
123+
jsonNodeResult.forEach(jsonNode -> assertFalse(jsonNode.isEmpty()));
124+
}
125+
103126
// Test to ensure that we can check property ordering defaults...
104127
@Test
105128
public void testConfigForPropertySorting() throws Exception

0 commit comments

Comments
 (0)