Class JsonUtils

java.lang.Object
host.anzo.commons.utils.JsonUtils

public class JsonUtils extends Object
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 Classes
    Modifier and Type
    Class
    Description
    static class 
    Custom deserializer for Boolean values that can handle various string representations and null values.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Converts a Java object to a JSON string.
    static String
    dataToJson(@NotNull spark.Response response, Object data)
    Converts a Java object to a JSON string and sets the response type to application/json.
    static <T> T
    jsonToObject(String body, Class<T> clazz)
    Converts a JSON string to a Java object of the specified class.
    static <T> T
    jsonToObject(@NotNull spark.Request request, Class<T> clazz)
    Converts the body of a Spark Request to a Java object of the specified class.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 body
      clazz - 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 convert
      clazz - the class of the object to convert to
      Returns:
      the converted object
      Throws:
      com.fasterxml.jackson.core.JsonProcessingException - if the JSON processing fails
    • dataToJson

      public static String dataToJson(@NotNull @NotNull spark.Response response, Object data)
      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 type
      data - the object to convert to JSON
      Returns:
      the JSON string representation of the object
    • dataToJson

      public static String dataToJson(Object data)
      Converts a Java object to a JSON string.
      Parameters:
      data - the object to convert to JSON
      Returns:
      the JSON string representation of the object