File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed
main/java/tools/jackson/databind
test/java/tools/jackson/databind Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 1010import java .util .Map ;
1111import java .util .concurrent .ConcurrentHashMap ;
1212import java .util .concurrent .atomic .AtomicReference ;
13+ import java .util .stream .Collector ;
1314
1415import tools .jackson .core .*;
1516import 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
Original file line number Diff line number Diff line change 77import java .nio .charset .StandardCharsets ;
88import java .nio .file .*;
99import java .util .*;
10+ import java .util .stream .IntStream ;
1011import java .util .stream .Collectors ;
1112import 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
You can’t perform that action at this time.
0 commit comments