Skip to content

Commit 567e089

Browse files
authored
Add JacksonCollectors with toArrayNode() implementation (#4709)
1 parent 71ac7c3 commit 567e089

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

release-notes/CREDITS-2.x

+5
Original file line numberDiff line numberDiff line change
@@ -1829,3 +1829,8 @@ Eduard Gomoliako (Gems@github)
18291829
Mathijs Vogelzang (mathijs81@github)
18301830
* Reported #4678: Java records don't serialize with `MapperFeature.REQUIRE_SETTERS_FOR_GETTERS`
18311831
(2.18.0)
1832+
1833+
Rikkarth (rikkarth@github)
1834+
* Contributed #4709: Add `JacksonCollectors` with `toArrayNode()` implementation
1835+
(2.18.0)
1836+

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ Project: jackson-databind
7979
(reported by @lnthai2002)
8080
#4699: Add extra `writeNumber()` method in `TokenBuffer`
8181
(contributed by @pjfanning)
82+
#4709: Add `JacksonCollectors` with `toArrayNode()` implementation
83+
(contributed by @rikkarth)
8284
8385
2.17.2 (05-Jul-2024)
8486
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fasterxml.jackson.databind.util;
2+
3+
import java.util.stream.Collector;
4+
5+
import com.fasterxml.jackson.databind.JsonNode;
6+
import com.fasterxml.jackson.databind.node.ArrayNode;
7+
import com.fasterxml.jackson.databind.node.JsonNodeCreator;
8+
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
9+
10+
/**
11+
* Utility class that provides custom {@link Collector} implementations to support Stream operations.
12+
* <p>
13+
* This class is not meant to be instantiated and serves only as a utility class.
14+
* </p>
15+
*
16+
* @since 2.18
17+
*/
18+
public abstract class JacksonCollectors {
19+
/**
20+
* Creates a {@link Collector} that collects {@link JsonNode} elements into an {@link ArrayNode}.
21+
* <p>
22+
* This method uses a {@link JsonNodeFactory} to create an empty {@link ArrayNode} and then adds each
23+
* {@link JsonNode} to it.
24+
* </p>
25+
*<p>
26+
* Short-cut to
27+
*{@code toArrayNode(JsonNodeFactory.instance}}
28+
*
29+
* @return a {@link Collector} that collects {@link JsonNode} elements into an {@link ArrayNode}
30+
*/
31+
public static Collector<JsonNode, ArrayNode, ArrayNode> toArrayNode() {
32+
return toArrayNode(JsonNodeFactory.instance);
33+
}
34+
35+
/**
36+
* Creates a {@link Collector} that collects {@link JsonNode} elements into an {@link ArrayNode}.
37+
* <p>
38+
* This method uses a {@link JsonNodeFactory} to create an empty {@link ArrayNode} and then adds each
39+
* {@link JsonNode} to it.
40+
* </p>
41+
*
42+
* @param nodeCreator Factory for constructing {@link ArrayNode} to contain nodes in
43+
*
44+
* @return a {@link Collector} that collects {@link JsonNode} elements into an {@link ArrayNode}
45+
*/
46+
public static Collector<JsonNode, ArrayNode, ArrayNode> toArrayNode(JsonNodeCreator nodeCreator) {
47+
return Collector.of(
48+
nodeCreator::arrayNode, // supplier
49+
ArrayNode::add, // accumulator
50+
ArrayNode::addAll // combiner
51+
);
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fasterxml.jackson.databind.util;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
6+
import com.fasterxml.jackson.databind.JsonNode;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.databind.node.ObjectNode;
9+
import java.util.stream.IntStream;
10+
import org.junit.jupiter.api.Test;
11+
12+
public class JacksonCollectorsTest {
13+
14+
@Test
15+
public void testToArrayNode()
16+
{
17+
final ObjectMapper objectMapper = new ObjectMapper();
18+
19+
final JsonNode jsonNodeResult = IntStream.range(0, 10)
20+
.mapToObj(i -> {
21+
ObjectNode objectNode = objectMapper.createObjectNode();
22+
objectNode.put("testString", "example");
23+
objectNode.put("testNumber", i);
24+
objectNode.put("testBoolean", true);
25+
26+
return objectNode;
27+
})
28+
.collect(JacksonCollectors.toArrayNode());
29+
30+
assertEquals(10, jsonNodeResult.size());
31+
jsonNodeResult.forEach(jsonNode -> assertFalse(jsonNode.isEmpty()));
32+
}
33+
}

0 commit comments

Comments
 (0)