Package net.ygbstudio.jbrave.core.utils
Class JsonSupport
java.lang.Object
net.ygbstudio.jbrave.core.utils.JsonSupport
JsonSupport provides JSON serialization and deserialization support using Jackson.
This class exists to reuse common JSON operations across different classes without code duplication. It also provides a pre-configured ObjectMapper instance for reuse, consistency and maximum performance gain.
- Author:
- Yoham Gabriel @ YGB Studio
-
Method Summary
Modifier and TypeMethodDescriptionstatic tools.jackson.databind.ObjectMapperConfigures and returns a pre-configured ObjectMapper instance.static tools.jackson.databind.ObjectMapperReturns the configured ObjectMapper instance that is reused throughout the application.static <T> TjsonReader(Reader source, Class<T> clazz) Reads a JSON string from a Reader and deserializes it into an object of the specified class.static <T> TobjectFromJson(String json, Class<T> clazz) Deserializes a JSON string into an object of the specified class.static <T> TreadJsonFs(File jsonFile, Class<T> clazz) Reads a JSON file from the filesystem and deserializes it into an object of the specified class.static StringtoJsonString(Object obj) Serializes an object into a JSON string.static <T> voidwriteJsonFs(File jsonFile, T obj) Serializes an object and writes it to a JSON file on the filesystem.
-
Method Details
-
configureMapper
public static tools.jackson.databind.ObjectMapper configureMapper()Configures and returns a pre-configured ObjectMapper instance.This instance is configured to:
- Indent output for better readability.
- Fail on empty beans during serialization.
- Ignore unknown properties during deserialization.
The configured ObjectMapper is intended for reuse across different classes to ensure consistency and performance.
- Returns:
- a configured ObjectMapper instance
-
getMapper
public static tools.jackson.databind.ObjectMapper getMapper()Returns the configured ObjectMapper instance that is reused throughout the application.- Returns:
- the configured ObjectMapper instance
-
objectFromJson
Deserializes a JSON string into an object of the specified class.- Type Parameters:
T- the type of the object to deserialize into- Parameters:
json- the JSON string to deserializeclazz- the class of the object to deserialize into- Returns:
- the deserialized object
-
toJsonString
Serializes an object into a JSON string.- Parameters:
obj- the object to serialize- Returns:
- the JSON string representation of the object
-
readJsonFs
Reads a JSON file from the filesystem and deserializes it into an object of the specified class.- Type Parameters:
T- the type of the object to deserialize into- Parameters:
jsonFile- the JSON file to readclazz- the class of the object to deserialize into- Returns:
- the deserialized object
-
jsonReader
Reads a JSON string from a Reader and deserializes it into an object of the specified class.- Type Parameters:
T- the type of the object to deserialize into- Parameters:
source- reader instance for the JSON string to read fromclazz- the class of the object to deserialize into- Returns:
- the deserialized object
-
writeJsonFs
Serializes an object and writes it to a JSON file on the filesystem.- Type Parameters:
T- the type of the object to serialize- Parameters:
jsonFile- the JSON file to write toobj- the object to serialize
-