Package common.utils
Class JsonUtils
java.lang.Object
common.utils.JsonUtils
Collection of utilities for working with JSON.
- Author:
- woodser
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
deserialize
(com.fasterxml.jackson.databind.ObjectMapper mapper, String json, com.fasterxml.jackson.core.type.TypeReference<T> type) Deserializes JSON to a parameterized type.static <T> T
deserialize
(com.fasterxml.jackson.databind.ObjectMapper mapper, String json, Class<T> clazz) Deserializes JSON to a specific class.static <T> T
deserialize
(String json, com.fasterxml.jackson.core.type.TypeReference<T> type) Deserializes JSON to a parameterized type.static <T> T
deserialize
(String json, Class<T> clazz) Deserializes JSON to a specific class.static String
Serializes an object to a JSON string.static String
Serializes an object to a JSON string.Converts an object to a map.Converts a JSON string to a map.Converts an object to a map.Converts a JSON string to a map.
-
Constructor Details
-
JsonUtils
public JsonUtils()
-
-
Method Details
-
serialize
Serializes an object to a JSON string.- Parameters:
obj
- is the object to serialize- Returns:
- String is the object serialized to a JSON string
-
serialize
Serializes an object to a JSON string.- Parameters:
mapper
- is the jackson object mapper to useobj
- is the object to serialize- Returns:
- String is the object serialized to a JSON string
-
deserialize
Deserializes JSON to a specific class.- Type Parameters:
T
- templated type- Parameters:
json
- is the JSON to deserializeclazz
- specifies the class to deserialize to- Returns:
- T is the object deserialized from JSON to the given class
-
deserialize
public static <T> T deserialize(com.fasterxml.jackson.databind.ObjectMapper mapper, String json, Class<T> clazz) Deserializes JSON to a specific class.- Type Parameters:
T
- templated type- Parameters:
mapper
- is the jackson object mapper to usejson
- is the JSON to deserializeclazz
- specifies the class to deserialize to- Returns:
- T is the object deserialized from JSON to the given class
-
deserialize
Deserializes JSON to a parameterized type.- Type Parameters:
T
- the map, list, object, etc to deserialize- Parameters:
json
- is the JSON to deserializetype
- is the parameterized type to deserialize to (e.g. new TypeReference<Map<String, Object>>(){})- Returns:
- T is the object deserialized from JSON to the given parameterized type
-
deserialize
public static <T> T deserialize(com.fasterxml.jackson.databind.ObjectMapper mapper, String json, com.fasterxml.jackson.core.type.TypeReference<T> type) Deserializes JSON to a parameterized type.- Type Parameters:
T
- the map, list, object, etc to deserialize- Parameters:
mapper
- is the jackson object mapper to usejson
- is the JSON to deserializetype
- is the parameterized type to deserialize to (e.g. new TypeReference<Map<String, Object>>(){})- Returns:
- T is the object deserialized from JSON to the given parameterized type
-
toMap
Converts a JSON string to a map.- Parameters:
json
- is the string to convert to a map- Returns:
- the json string converted to a map
-
toMap
public static Map<String,Object> toMap(com.fasterxml.jackson.databind.ObjectMapper mapper, String json) Converts a JSON string to a map.- Parameters:
mapper
- is the jackson object mapper to usejson
- is the string to convert to a map- Returns:
- is the json string converted to a map
-
toMap
Converts an object to a map.- Parameters:
obj
- is the object to a convert to a map- Returns:
- the object converted to a map
-
toMap
public static Map<String,Object> toMap(com.fasterxml.jackson.databind.ObjectMapper mapper, Object obj) Converts an object to a map.- Parameters:
mapper
- is the jackson object mapper to useobj
- is the object to a convert to a map- Returns:
- the object converted to a map
-