-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
ObjectMapper.toJsonNode() Collector Stream Support #4691
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
Closed
rikkarth
wants to merge
6
commits into
FasterXML:master
from
rikkarth:feat/toJsonNodeStreamCollector
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4fcaf6f
ObjectMapper.toJsonNode base implementation
rikkarth 84b9ff1
implementation moved to util.JacksonCollectors
rikkarth 4ad6737
cleanup, toJsonNode minor change
rikkarth 0c20512
cleanup, toJsonNode minor change
rikkarth e116314
Merge branch 'master' into feat/toJsonNodeStreamCollector
cowtowncoder 66024d9
Minor cleanup
cowtowncoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/tools/jackson/databind/util/JacksonCollectors.java
This file contains hidden or 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,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.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> | ||
* | ||
* @return a {@link Collector} that collects {@link JsonNode} elements into an {@link ArrayNode} | ||
*/ | ||
public static Collector<JsonNode, ArrayNode, ArrayNode> toJsonNode() { | ||
return toJsonNode(JsonNodeFactory.instance); | ||
} | ||
|
||
public static Collector<JsonNode, ArrayNode, ArrayNode> toJsonNode(JsonNodeCreator nodeCreator) { | ||
return Collector.of( | ||
nodeCreator::arrayNode, // supplier | ||
ArrayNode::add, // accumulator | ||
ArrayNode::addAll // combiner | ||
); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/test/java/tools/jackson/databind/util/JacksonCollectorsTest.java
This file contains hidden or 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,33 @@ | ||
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()); | ||
|
||
assertEquals(10, jsonNodeResult.size()); | ||
jsonNodeResult.forEach(jsonNode -> assertFalse(jsonNode.isEmpty())); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.