Package utils

Class JsonUtils

java.lang.Object
utils.JsonUtils

public class JsonUtils extends Object
Utility class for JSON operations using Jackson ObjectMapper. Provides methods for converting between JSON strings, input streams, and Java objects.
  • Constructor Details

    • JsonUtils

      public JsonUtils()
  • Method Details

    • jsonToMap

      public static Map<String,Object> jsonToMap(String json)
      Converts a JSON string to a Map.
      Parameters:
      json - the JSON string to convert
      Returns:
      a Map representation of the JSON, or an empty map if conversion fails
    • jsonToMap

      public static Map<String,Object> jsonToMap(InputStream inputStream)
      Converts a JSON input stream to a Map.
      Parameters:
      inputStream - the input stream containing JSON data
      Returns:
      a Map representation of the JSON, or an empty map if conversion fails
    • fromJson

      public static <T> T fromJson(String json, Class<T> clazz)
      Converts a JSON string to an object of the specified class.
      Type Parameters:
      T - the type of object to convert to
      Parameters:
      json - the JSON string to convert
      clazz - the class of the target object
      Returns:
      an instance of the target class, or null if conversion fails
    • fromJson

      public static <T> T fromJson(String filename, Class<T> clazz, boolean isFile)
      Reads and converts a JSON file from the classpath to an object of the specified class.
      Type Parameters:
      T - the type of object to convert to
      Parameters:
      filename - the name of the JSON file in the classpath
      clazz - the class of the target object
      isFile - whether the input is a file (unused parameter)
      Returns:
      an instance of the target class, or null if the file is not found or conversion fails