Package host.anzo.commons.utils
Class JsonUtils
java.lang.Object
host.anzo.commons.utils.JsonUtils
Utility class for handling JSON serialization and deserialization using Jackson.
This class provides methods to convert JSON data to Java objects and vice versa.
- Since:
- 08.06.2017
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classCustom deserializer for Boolean values that can handle various string representations and null values. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringdataToJson(Object data) Converts a Java object to a JSON string.static StringdataToJson(@NotNull spark.Response response, Object data) Converts a Java object to a JSON string and sets the response type to application/json.static <T> TjsonToObject(String body, Class<T> clazz) Converts a JSON string to a Java object of the specified class.static <T> TjsonToObject(@NotNull spark.Request request, Class<T> clazz) Converts the body of a Spark Request to a Java object of the specified class.
-
Constructor Details
-
JsonUtils
public JsonUtils()
-
-
Method Details
-
jsonToObject
public static <T> T jsonToObject(@NotNull @NotNull spark.Request request, Class<T> clazz) throws com.fasterxml.jackson.core.JsonProcessingException Converts the body of a Spark Request to a Java object of the specified class.- Type Parameters:
T- the type of the object- Parameters:
request- the Spark Request containing the JSON bodyclazz- the class of the object to convert to- Returns:
- the converted object
- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if the JSON processing fails
-
jsonToObject
public static <T> T jsonToObject(String body, Class<T> clazz) throws com.fasterxml.jackson.core.JsonProcessingException Converts a JSON string to a Java object of the specified class.- Type Parameters:
T- the type of the object- Parameters:
body- the JSON string to convertclazz- the class of the object to convert to- Returns:
- the converted object
- Throws:
com.fasterxml.jackson.core.JsonProcessingException- if the JSON processing fails
-
dataToJson
Converts a Java object to a JSON string and sets the response type to application/json.- Parameters:
response- the Spark Response to set the content typedata- the object to convert to JSON- Returns:
- the JSON string representation of the object
-
dataToJson
Converts a Java object to a JSON string.- Parameters:
data- the object to convert to JSON- Returns:
- the JSON string representation of the object
-