Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjectMapper.toJsonNode() Collector Stream Support #4691

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/tools/jackson/databind/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collector;

import tools.jackson.core.*;
import tools.jackson.core.exc.StreamReadException;
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/tools/jackson/databind/util/JacksonCollectors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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.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>
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved
*/
public class JacksonCollectors {

private JacksonCollectors() {
throw new UnsupportedOperationException("Utility class cannot be instantiated.");
}

/**
* 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>
*
* @return a {@link Collector} that collects {@link JsonNode} elements into an {@link ArrayNode}
*/
public static Collector<JsonNode, ArrayNode, ArrayNode> toJsonNode() {
final JsonNodeFactory jsonNodeFactory = new JsonNodeFactory();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although for most use the default JsonNodeFactory is fine, I think it'd better to have a variant that takes in JsonNodeFactory (or JsonNodeCreator maybe, which it implements), and then this overload that uses

JsonNodeFactory.instance

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check now please?


return Collector.of(
jsonNodeFactory::arrayNode, // supplier
ArrayNode::add, // accumulator
ArrayNode::addAll // combiner
);
}
}
1 change: 1 addition & 0 deletions src/test/java/tools/jackson/databind/ObjectMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Collectors;
import java.util.zip.ZipOutputStream;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tools.jackson.databind.util;

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

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;

public class JacksonCollectorsTest {

@Test
public void testToJsonNode()
{
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.toJsonNode());

System.out.println(jsonNodeResult.toPrettyString());
rikkarth marked this conversation as resolved.
Show resolved Hide resolved

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