Skip to content

Commit 66004a3

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

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-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: 22 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,27 @@ public void testProps()
100101
assertSame(nf, m.getNodeFactory());
101102
}
102103

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

0 commit comments

Comments
 (0)