Class JsonConverters

java.lang.Object
org.tudalgo.algoutils.tutor.general.json.JsonConverters

public class JsonConverters extends Object
A class that contains methods to convert JsonNodes to java objects.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Map<String,Function<com.fasterxml.jackson.databind.JsonNode,?>>
    The default converters that are used if no custom converters are given.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    convert(com.fasterxml.jackson.databind.JsonNode node, String key, Class<T> type, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
    Converts a JsonNode to a T by applying the given mapper to the json node.
    static <T> T
    convert(com.fasterxml.jackson.databind.JsonNode node, String key, Class<T> type, com.fasterxml.jackson.databind.ObjectMapper objectMapper, Map<String,Function<com.fasterxml.jackson.databind.JsonNode,?>> converters)
    Converts a JsonNode to a T by applying the given mapper to the json node.
    toIntMap(com.fasterxml.jackson.databind.JsonNode jsonNode)
    Converts a JsonNode to a Map of Integers and Integers by parsing the keys and values of the json node.
    static <T> List<T>
    toList(com.fasterxml.jackson.databind.JsonNode jsonNode, Function<com.fasterxml.jackson.databind.JsonNode,T> mapper)
    Converts a JsonNode to a List of Ts by applying the given mapper to each element of the json node.
    static <K, V> Map<K,V>
    toMap(com.fasterxml.jackson.databind.JsonNode jsonNode, Function<String,K> keyMapper, Function<com.fasterxml.jackson.databind.JsonNode,V> valueMapper)
    Converts a JsonNode to a Map of Ks and Vs by applying the given keyMapper to each key and the valueMapper to each value of the json node.

    Methods inherited from class java.lang.Object

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

    • DEFAULT_CONVERTERS

      public static final Map<String,Function<com.fasterxml.jackson.databind.JsonNode,?>> DEFAULT_CONVERTERS
      The default converters that are used if no custom converters are given.
  • Constructor Details

    • JsonConverters

      public JsonConverters()
  • Method Details

    • toList

      public static <T> List<T> toList(com.fasterxml.jackson.databind.JsonNode jsonNode, Function<com.fasterxml.jackson.databind.JsonNode,T> mapper)
      Converts a JsonNode to a List of Ts by applying the given mapper to each element of the json node.
      Parameters:
      jsonNode - the json node to convert.
      mapper - the mapper that is applied to each element of the json node.
    • toMap

      public static <K, V> Map<K,V> toMap(com.fasterxml.jackson.databind.JsonNode jsonNode, Function<String,K> keyMapper, Function<com.fasterxml.jackson.databind.JsonNode,V> valueMapper)
      Converts a JsonNode to a Map of Ks and Vs by applying the given keyMapper to each key and the valueMapper to each value of the json node.
      Type Parameters:
      K - the type of the keys of the map.
      V - the type of the values of the map.
      Parameters:
      jsonNode - the json node to convert.
      keyMapper - the mapper that is applied to each key of the json node.
      valueMapper - the mapper that is applied to each value of the json node.
      Returns:
      the converted map.
    • toIntMap

      public static Map<Integer,Integer> toIntMap(com.fasterxml.jackson.databind.JsonNode jsonNode)
      Converts a JsonNode to a Map of Integers and Integers by parsing the keys and values of the json node.
      Parameters:
      jsonNode - the json node to convert.
      Returns:
      the converted map.
    • convert

      public static <T> T convert(com.fasterxml.jackson.databind.JsonNode node, String key, Class<T> type, com.fasterxml.jackson.databind.ObjectMapper objectMapper, Map<String,Function<com.fasterxml.jackson.databind.JsonNode,?>> converters)
      Converts a JsonNode to a T by applying the given mapper to the json node.
      Type Parameters:
      T - the type of the object to convert to.
      Parameters:
      node - the json node to convert.
      key - the key of the json node.
      type - the type of the object to convert to.
      objectMapper - the object mapper to use
      converters - the custom converters to use
      Returns:
      the converted object.
    • convert

      public <T> T convert(com.fasterxml.jackson.databind.JsonNode node, String key, Class<T> type, com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Converts a JsonNode to a T by applying the given mapper to the json node.
      Type Parameters:
      T - the type of the object to convert to.
      Parameters:
      node - the json node to convert.
      key - the key of the json node.
      type - the type of the object to convert to.
      objectMapper - the object mapper to use.
      Returns:
      the converted object.