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 SummaryConstructors
- 
Method SummaryModifier 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- 
JacksonJsUtilspublic JacksonJsUtils()
 
- 
- 
Method Details- 
toArrayNodepublic 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 to- create a new array.
 
- 
toObjectNodepublic 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 via- ObjectMapper.createObjectNode()
- keyMapper- maps an element to its key
- valueMapper- maps an element to its corresponding JSON. See notes on- toArrayNode(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 in- Map.merge(Object, Object, java.util.function.BiFunction). In the- default case, it forbids to have duplicated keys with different values.
 
- 
toObjectNodepublic 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.
 
-