Class JsonUtils

java.lang.Object
land.oras.utils.JsonUtils

@NullMarked public final class JsonUtils extends Object
Utility class for JSON operations. Use Gson internally for JSON operations
  • Method Details

    • toJson

      public static String toJson(Object object)
      Convert an object to a JSON string
      Parameters:
      object - The object to convert
      Returns:
      The JSON string
    • fromJson

      public static <T> T fromJson(String json, Class<T> clazz)
      Convert a JSON string to an object
      Type Parameters:
      T - The type of the object
      Parameters:
      json - The JSON string
      clazz - The class of the object
      Returns:
      The object
    • fromJson

      public static <T> T fromJson(Path path, Class<T> clazz)
      Convert a JSON string to an object. Be careful when using this utility since the original JSON string is lost and might change content digest. Use this method only when the JSON string is not needed anymore or when the digest of the JSON string is not important.
      Type Parameters:
      T - The type of the object
      Parameters:
      path - The path to the JSON file
      clazz - The class of the object
      Returns:
      The object
    • fromJson

      public static <T> T fromJson(Path path, Type type)
      Converts the contents of a JSON file to an object of the specified type.
      Type Parameters:
      T - The type of the object to be returned.
      Parameters:
      path - The Path to the JSON file to be read.
      type - The Type representing the class of the object to be deserialized.
      Returns:
      An object of type T deserialized from the JSON file.
      Throws:
      OrasException - If an I/O error occurs while reading the file or the JSON is invalid.
    • fromJson

      public static <T> T fromJson(Reader reader, Type type)
      Deserializes the contents of a JSON input to an object of the specified type.
      Type Parameters:
      T - The type of the object to be returned.
      Parameters:
      reader - The Reader from which the JSON content is read.
      type - The Type representing the target object type to be deserialized.
      Returns:
      An object of type T deserialized from the JSON content.
      Throws:
      OrasException - If an error occurs while reading the input or the JSON format is invalid.
    • readFile

      public static String readFile(Path path)
      Read a file and return the content as a string
      Parameters:
      path - The path to the file
      Returns:
      The content of the file