Package net.ygbstudio.powerwp4j.utils
Class JsonSupport
java.lang.Object
net.ygbstudio.powerwp4j.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.
-
Method Summary
Modifier and TypeMethodDescriptionstatic tools.jackson.databind.ObjectMapperConfigures and returns a pre-configured ObjectMapper instance.static <T> Optional<T> deserialize(@NotNull HttpResponse<String> response, Class<T> clazz) Deserializes a generic response from an HttpResponse.static <T> Optional<T> deserialize(@NotNull HttpResponse<String> response, tools.jackson.core.type.TypeReference<T> typeRef) Deserializes a generic list response from an HttpResponse.static tools.jackson.databind.ObjectMapperReturns the configured ObjectMapper instance that is reused throughout the application.static tools.jackson.databind.JsonNodegetTreeNode(File jsonFile) Reads a JSON file from the filesystem and parses it into a JsonNode.static tools.jackson.databind.JsonNodegetTreeNode(String jsonString) Parses a JSON string into a JsonNode.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> TreadJsonFs(File jsonFile, Class<T> clazz) Reads a JSON file from the filesystem and deserializes it into an object of the specified class.static <T> TreadValueFromJson(String json, Class<T> clazz) Deserializes a JSON string 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.
- Write dates as timestamps.
- Ignore unknown properties during deserialization.
- Accept empty strings as null objects 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
-
readValueFromJson
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
-
getTreeNode
Parses a JSON string into a JsonNode.- Parameters:
jsonString- the JSON string to parse- Returns:
- the parsed JsonNode
-
getTreeNode
Reads a JSON file from the filesystem and parses it into a JsonNode.- Parameters:
jsonFile- the JSON file to read- Returns:
- the parsed JsonNode
-
deserialize
public static <T> Optional<T> deserialize(@NotNull @NotNull HttpResponse<String> response, Class<T> clazz) Deserializes a generic response from an HttpResponse.- Type Parameters:
T- the type of the object to deserialize into- Parameters:
response- the HttpResponse to deserializeclazz- the class of the object to deserialize into- Returns:
- an Optional containing the deserialized object, or empty if status >= 400 or generic error
-
deserialize
public static <T> Optional<T> deserialize(@NotNull @NotNull HttpResponse<String> response, tools.jackson.core.type.TypeReference<T> typeRef) Deserializes a generic list response from an HttpResponse.- Type Parameters:
T- the type of the object to deserialize into- Parameters:
response- the HttpResponse to deserializetypeRef- the TypeReference of the object to deserialize into- Returns:
- an Optional containing the deserialized object, or empty if status >= 400 or generic error
-