Skip to content

Commit

Permalink
Add JacksonCollectors with toArrayNode() implementation (#4709)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkarth authored Sep 20, 2024
1 parent 71ac7c3 commit 567e089
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
5 changes: 5 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -1829,3 +1829,8 @@ Eduard Gomoliako (Gems@github)
Mathijs Vogelzang (mathijs81@github)
* Reported #4678: Java records don't serialize with `MapperFeature.REQUIRE_SETTERS_FOR_GETTERS`
(2.18.0)

Rikkarth (rikkarth@github)
* Contributed #4709: Add `JacksonCollectors` with `toArrayNode()` implementation
(2.18.0)

2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ Project: jackson-databind
(reported by @lnthai2002)
#4699: Add extra `writeNumber()` method in `TokenBuffer`
(contributed by @pjfanning)
#4709: Add `JacksonCollectors` with `toArrayNode()` implementation
(contributed by @rikkarth)
2.17.2 (05-Jul-2024)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.fasterxml.jackson.databind.util;

import java.util.stream.Collector;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeCreator;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;

/**
* Utility class that provides custom {@link Collector} implementations to support Stream operations.
* <p>
* This class is not meant to be instantiated and serves only as a utility class.
* </p>
*
* @since 2.18
*/
public abstract class JacksonCollectors {
/**
* Creates a {@link Collector} that collects {@link JsonNode} elements into an {@link ArrayNode}.
* <p>
* This method uses a {@link JsonNodeFactory} to create an empty {@link ArrayNode} and then adds each
* {@link JsonNode} to it.
* </p>
*<p>
* Short-cut to
*{@code toArrayNode(JsonNodeFactory.instance}}
*
* @return a {@link Collector} that collects {@link JsonNode} elements into an {@link ArrayNode}
*/
public static Collector<JsonNode, ArrayNode, ArrayNode> toArrayNode() {
return toArrayNode(JsonNodeFactory.instance);
}

/**
* Creates a {@link Collector} that collects {@link JsonNode} elements into an {@link ArrayNode}.
* <p>
* This method uses a {@link JsonNodeFactory} to create an empty {@link ArrayNode} and then adds each
* {@link JsonNode} to it.
* </p>
*
* @param nodeCreator Factory for constructing {@link ArrayNode} to contain nodes in
*
* @return a {@link Collector} that collects {@link JsonNode} elements into an {@link ArrayNode}
*/
public static Collector<JsonNode, ArrayNode, ArrayNode> toArrayNode(JsonNodeCreator nodeCreator) {
return Collector.of(
nodeCreator::arrayNode, // supplier
ArrayNode::add, // accumulator
ArrayNode::addAll // combiner
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.fasterxml.jackson.databind.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;

public class JacksonCollectorsTest {

@Test
public void testToArrayNode()
{
final ObjectMapper objectMapper = new ObjectMapper();

final JsonNode jsonNodeResult = IntStream.range(0, 10)
.mapToObj(i -> {
ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put("testString", "example");
objectNode.put("testNumber", i);
objectNode.put("testBoolean", true);

return objectNode;
})
.collect(JacksonCollectors.toArrayNode());

assertEquals(10, jsonNodeResult.size());
jsonNodeResult.forEach(jsonNode -> assertFalse(jsonNode.isEmpty()));
}
}

0 comments on commit 567e089

Please sign in to comment.