Class JsonConverters
java.lang.Object
org.tudalgo.algoutils.tutor.general.json.JsonConverters
A class that contains methods to convert JsonNodes to java objects.
-
Field Summary
FieldsModifier and TypeFieldDescriptionThe default converters that are used if no custom converters are given. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> Tconvert(com.fasterxml.jackson.databind.JsonNode node, String key, Class<T> type, com.fasterxml.jackson.databind.ObjectMapper objectMapper) Converts a JsonNode to aTby applying the given mapper to the json node.static <T> Tconvert(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 aTby applying the given mapper to the json node.toIntMap(com.fasterxml.jackson.databind.JsonNode jsonNode) Converts a JsonNode to a Map ofIntegers andIntegers 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) 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 ofKs andVs by applying the given keyMapper to each key and the valueMapper to each value of the json node.
-
Field Details
-
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) - 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 ofKs andVs 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
Converts a JsonNode to a Map ofIntegers andIntegers 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 aTby 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 useconverters- 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 aTby 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.
-