From 8f73f9cd90a1e0d98d0c09b1b52924a245693ffa Mon Sep 17 00:00:00 2001 From: Marco Brandizi Date: Mon, 13 May 2024 17:29:29 +0100 Subject: [PATCH] Adding JacksonJsUtils.toArrayNode() --- pom.xml | 1 + revision-history.md | 5 +- .../ac/ebi/utils/opt/json/JacksonJsUtils.java | 63 +++++++++++++++++++ 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 src/main/java/uk/ac/ebi/utils/opt/json/JacksonJsUtils.java diff --git a/pom.xml b/pom.xml index e581c1e3a..46fd5f186 100644 --- a/pom.xml +++ b/pom.xml @@ -221,6 +221,7 @@ com.fasterxml.jackson.dataformat diff --git a/revision-history.md b/revision-history.md index d4f132cb9..7625e4b1e 100644 --- a/revision-history.md +++ b/revision-history.md @@ -1,12 +1,13 @@ # Revision History -*This file was last revised on 2024-04-15*. **Please keep this note updated**. +*This file was last revised on 2024-05-13*. **Please keep this note updated**. ## 14.1-SNAPSHOT * `ChainExecutor.andThen()` added (to be tested). * `StreamUtils.sampleStream()` added. * `uk.ac.ebi.utils.statistics.FisherExact` imported from the USeq project. -* `CollectionUtils`, adding functions to copy collections and make them read-only. +* `CollectionUtils`, adding functions to copy collections and making them read-only. +* `JacksonJsUtils.toArrayNode()` added. ## 14.0.1 * Various dependency upgrades. diff --git a/src/main/java/uk/ac/ebi/utils/opt/json/JacksonJsUtils.java b/src/main/java/uk/ac/ebi/utils/opt/json/JacksonJsUtils.java new file mode 100644 index 000000000..da791056b --- /dev/null +++ b/src/main/java/uk/ac/ebi/utils/opt/json/JacksonJsUtils.java @@ -0,0 +1,63 @@ +package uk.ac.ebi.utils.opt.json; + +import static java.util.stream.Collector.Characteristics.IDENTITY_FINISH; +import static java.util.stream.Collector.Characteristics.UNORDERED; + +import java.util.function.BiConsumer; +import java.util.stream.Collector; +import java.util.stream.Collector.Characteristics; +import java.util.stream.Stream; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; + +/** + * JSON utils based on the Jackson library. + * + * @author Marco Brandizi + *
Date:
13 May 2024
+ * + */ +public class JacksonJsUtils +{ + + /** + * Returns a collector (i.e., for {@link Stream#collect(Collector)} and the like) that + * collects the elements of type T into an {@link ArrayNode}. + * + * This is a serialised collector, which requires synchronisation when elements are accumulated (ie, + * it doesn't have {@link Characteristics#CONCURRENT}. + * + * At the same time, it's an {@link Characteristics#UNORDERED} and {@link Characteristics#IDENTITY_FINISH} + * collector. + * + * @param objectMapper the mapper where the {@link ObjectMapper#createArrayNode()} is taken. + * @param accumulator method to add an element to a node. This is typically an addXXX() method + * from {@link ArrayNode}. + * + * TODO: tests + * + */ + public static Collector toArrayNode ( + ObjectMapper objectMapper, BiConsumer accumulator + ) + { + return Collector. of ( + objectMapper::createArrayNode, + accumulator, + ArrayNode::addAll, + UNORDERED + ); + } + + /** + * Flavour that uses {@link ArrayNode#add(JsonNode)} + */ + public static Collector toArrayNode ( + ObjectMapper objectMapper + ) + { + return toArrayNode ( objectMapper, ArrayNode::add ); + } +}