Package land.oras.utils
Class JsonUtils
java.lang.Object
land.oras.utils.JsonUtils
Utility class for JSON operations.
Use Gson internally for JSON operations
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
Deserializes the contents of a JSON input to an object of the specified type.static <T> T
Convert a JSON string to an objectstatic <T> T
Convert a JSON string to an object.static <T> T
Converts the contents of a JSON file to an object of the specified type.static String
Read a file and return the content as a stringstatic String
Convert an object to a JSON string
-
Method Details
-
toJson
Convert an object to a JSON string- Parameters:
object
- The object to convert- Returns:
- The JSON string
-
fromJson
Convert a JSON string to an object- Type Parameters:
T
- The type of the object- Parameters:
json
- The JSON stringclazz
- The class of the object- Returns:
- The object
-
fromJson
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 fileclazz
- The class of the object- Returns:
- The object
-
fromJson
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
- ThePath
to the JSON file to be read.type
- TheType
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
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
- TheReader
from which the JSON content is read.type
- TheType
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
Read a file and return the content as a string- Parameters:
path
- The path to the file- Returns:
- The content of the file
-