Package utils
Class JsonUtils
java.lang.Object
utils.JsonUtils
Utility class for JSON operations using Jackson ObjectMapper. Provides methods for converting
between JSON strings, input streams, and Java objects.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
Converts a JSON string to an object of the specified class.static <T> T
Reads and converts a JSON file from the classpath to an object of the specified class.jsonFileToMap
(String filename) Converts a JSON file from the classpath to a Map.Converts a JSON string to a Map.static String
Converts a Map to a JSON string.
-
Constructor Details
-
JsonUtils
public JsonUtils()
-
-
Method Details
-
jsonToMap
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
-
jsonFileToMap
Converts a JSON file from the classpath to a Map.- Parameters:
filename
- the name of the JSON file in the classpath- Returns:
- a Map representation of the JSON, or an empty map if conversion fails
-
mapToJson
Converts a Map to a JSON string.- Parameters:
map
- the Map- Returns:
- a JSON String representation of the Map, or an empty string if conversion fails
-
fromJson
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 convertclazz
- the class of the target object- Returns:
- an instance of the target class, or null if conversion fails
-
fromJson
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 classpathclazz
- the class of the target objectisFile
- 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
-