Class JacksonJsUtils

java.lang.Object
uk.ac.ebi.utils.opt.json.JacksonJsUtils

public class JacksonJsUtils extends Object
JSON utils based on the Jackson library.
Author:
Marco Brandizi
Date:
13 May 2024
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static 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., for Stream.collect(Collector) and the like) that collects the elements of type T into an ArrayNode.
    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 throws IllegalArgumentException when 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 an ObjectNode, by extracting keys and JSON mappings from the elements.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JacksonJsUtils

      public JacksonJsUtils()
  • Method Details

    • toArrayNode

      public static 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., for Stream.collect(Collector) and the like) that collects the elements of type T into an ArrayNode. This is a serialised collector, which requires synchronisation when elements are accumulated (ie, it doesn't have Collector.Characteristics.CONCURRENT. At the same time, it's an Collector.Characteristics.UNORDERED and Collector.Characteristics.IDENTITY_FINISH collector.

      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 a map step.
      Parameters:
      objectMapper - the Jackson object mapper, used to create a new array.
    • toObjectNode

      public 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 an ObjectNode, 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.
    • toObjectNode

      public 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 throws IllegalArgumentException when the same key yields different values.