File tree 2 files changed +49
-0
lines changed
main/java/tools/jackson/databind
test/java/tools/jackson/databind
2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 10
10
import java .util .Map ;
11
11
import java .util .concurrent .ConcurrentHashMap ;
12
12
import java .util .concurrent .atomic .AtomicReference ;
13
+ import java .util .stream .Collector ;
13
14
14
15
import tools .jackson .core .*;
15
16
import tools .jackson .core .exc .StreamReadException ;
@@ -563,6 +564,31 @@ public Collection<JacksonModule> getRegisteredModules() {
563
564
return _savedBuilderState .modules ();
564
565
}
565
566
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
+
566
592
/*
567
593
/**********************************************************************
568
594
/* Public API: constructing Parsers that are properly linked
Original file line number Diff line number Diff line change 7
7
import java .nio .charset .StandardCharsets ;
8
8
import java .nio .file .*;
9
9
import java .util .*;
10
+ import java .util .stream .IntStream ;
10
11
import java .util .stream .Collectors ;
11
12
import java .util .zip .ZipOutputStream ;
12
13
@@ -100,6 +101,28 @@ public void testProps()
100
101
assertSame (nf , m .getNodeFactory ());
101
102
}
102
103
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
+
103
126
// Test to ensure that we can check property ordering defaults...
104
127
@ Test
105
128
public void testConfigForPropertySorting () throws Exception
You can’t perform that action at this time.
0 commit comments