-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/main/java/tools/jackson/databind/util/JacksonCollectors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package tools.jackson.databind.util; | ||
|
||
import java.util.stream.Collector; | ||
|
||
import tools.jackson.databind.JsonNode; | ||
import tools.jackson.databind.node.ArrayNode; | ||
import tools.jackson.databind.node.JsonNodeCreator; | ||
import tools.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 | ||
); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/tools/jackson/databind/util/JacksonCollectorsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tools.jackson.databind.util; | ||
|
||
import java.util.stream.IntStream; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import tools.jackson.databind.JsonNode; | ||
import tools.jackson.databind.ObjectMapper; | ||
import tools.jackson.databind.node.ObjectNode; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
||
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())); | ||
} | ||
} |