forked from ISA-tools/jUtils
-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
bf398f6
commit 8f73f9c
Showing
3 changed files
with
67 additions
and
2 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
63 changes: 63 additions & 0 deletions
63
src/main/java/uk/ac/ebi/utils/opt/json/JacksonJsUtils.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,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 | ||
* <dl><dt>Date:</dt><dd>13 May 2024</dd></dl> | ||
* | ||
*/ | ||
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 <T> Collector<T, ArrayNode, ArrayNode> toArrayNode ( | ||
ObjectMapper objectMapper, BiConsumer<ArrayNode, T> accumulator | ||
) | ||
{ | ||
return Collector.<T, ArrayNode> of ( | ||
objectMapper::createArrayNode, | ||
accumulator, | ||
ArrayNode::addAll, | ||
UNORDERED | ||
); | ||
} | ||
|
||
/** | ||
* Flavour that uses {@link ArrayNode#add(JsonNode)} | ||
*/ | ||
public static Collector<JsonNode, ArrayNode, ArrayNode> toArrayNode ( | ||
ObjectMapper objectMapper | ||
) | ||
{ | ||
return toArrayNode ( objectMapper, ArrayNode::add ); | ||
} | ||
} |