Package uk.ac.ebi.utils.opt.json
Class JacksonJsUtils
java.lang.Object
uk.ac.ebi.utils.opt.json.JacksonJsUtils
JSON utils based on the Jackson library.
- Author:
- Marco Brandizi
- Date:
- 13 May 2024
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Collector<com.fasterxml.jackson.databind.JsonNode, com.fasterxml.jackson.databind.node.ArrayNode, com.fasterxml.jackson.databind.node.ArrayNode> toArrayNode(com.fasterxml.jackson.databind.ObjectMapper objectMapper) Returns a collector (i.e., forStream.collect(Collector)and the like) that collects the elements of type T into anArrayNode.static <T> Collector<T, com.fasterxml.jackson.databind.node.ObjectNode, com.fasterxml.jackson.databind.node.ObjectNode> toObjectNode(com.fasterxml.jackson.databind.ObjectMapper objectMapper, Function<T, String> keyMapper, Function<T, com.fasterxml.jackson.databind.JsonNode> valueMapper) Uses a value merger that throwsIllegalArgumentExceptionwhen the same key yields different values.static <T> Collector<T, com.fasterxml.jackson.databind.node.ObjectNode, com.fasterxml.jackson.databind.node.ObjectNode> toObjectNode(com.fasterxml.jackson.databind.ObjectMapper objectMapper, Function<T, String> keyMapper, Function<T, com.fasterxml.jackson.databind.JsonNode> valueMapper, BinaryOperator<com.fasterxml.jackson.databind.JsonNode> valuesMerger) Collector to convert a stream of elements into anObjectNode, by extracting keys and JSON mappings from the elements.
-
Constructor Details
-
JacksonJsUtils
public JacksonJsUtils()
-
-
Method Details
-
toArrayNode
public static Collector<com.fasterxml.jackson.databind.JsonNode,com.fasterxml.jackson.databind.node.ArrayNode, toArrayNodecom.fasterxml.jackson.databind.node.ArrayNode> (com.fasterxml.jackson.databind.ObjectMapper objectMapper) Returns a collector (i.e., forStream.collect(Collector)and the like) that collects the elements of type T into anArrayNode. This is a serialised collector, which requires synchronisation when elements are accumulated (ie, it doesn't haveCollector.Characteristics.CONCURRENT. At the same time, it's anCollector.Characteristics.UNORDEREDandCollector.Characteristics.IDENTITY_FINISHcollector.Note: to create JSON nodes out of plain value:
vat nodeFactory = jsmapperRO.getDeserializationConfig ().getNodeFactory (); nodeFactory.textNode ( "Hello, World" ); nodeFactory.numericNode ( 2.5 );
This can be used in amap step.- Parameters:
objectMapper- the Jackson object mapper, used tocreate a new array.
-
toObjectNode
public static <T> Collector<T,com.fasterxml.jackson.databind.node.ObjectNode, toObjectNodecom.fasterxml.jackson.databind.node.ObjectNode> (com.fasterxml.jackson.databind.ObjectMapper objectMapper, Function<T, String> keyMapper, Function<T, com.fasterxml.jackson.databind.JsonNode> valueMapper, BinaryOperator<com.fasterxml.jackson.databind.JsonNode> valuesMerger) Collector to convert a stream of elements into anObjectNode, by extracting keys and JSON mappings from the elements.- Type Parameters:
T- the kind of element (in the stream)- Parameters:
objectMapper- The usual Jackson JSON object mapper, used to create the return value viaObjectMapper.createObjectNode()keyMapper- maps an element to its keyvalueMapper- maps an element to its corresponding JSON. See notes ontoArrayNode(ObjectMapper)regarding how to convert plain values to JSON objects.valuesMerger- decides what to do in case of two values coming from the same key. This is the conceptual equivalent of the remapping function inMap.merge(Object, Object, java.util.function.BiFunction). In thedefault case, it forbids to have duplicated keys with different values.
-
toObjectNode
public static <T> Collector<T,com.fasterxml.jackson.databind.node.ObjectNode, toObjectNodecom.fasterxml.jackson.databind.node.ObjectNode> (com.fasterxml.jackson.databind.ObjectMapper objectMapper, Function<T, String> keyMapper, Function<T, com.fasterxml.jackson.databind.JsonNode> valueMapper) Uses a value merger that throwsIllegalArgumentExceptionwhen the same key yields different values.
-