Package com.acumenvelocity.ath.common
Class JacksonUtil
- java.lang.Object
-
- com.acumenvelocity.ath.common.JacksonUtil
-
public class JacksonUtil extends Object
Utility class for JSON/Java object conversion using Jackson library. Provides serialization and deserialization capabilities with custom handlers for Date and MongoDB ObjectId types. Supports both JSON and YAML formats.This utility handles special serialization formats commonly used with MongoDB where dates and object IDs may be represented as extended JSON objects.
- Since:
- 1.0
- Version:
- 1.0
- Author:
- Acumen Velocity
- See Also:
ObjectMapper,JsonNode
-
-
Constructor Summary
Constructors Constructor Description JacksonUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> TfromJson(String json, com.fasterxml.jackson.core.type.TypeReference<T> type)static <T> TfromJson(String json, Class<? extends T> objClass)Deserializes JSON string to an object of the specified class using the primary ObjectMapper.static <T> TfromJson2(String json, Class<? extends T> objClass)Deserializes JSON string to an object of the specified class using the alternative ObjectMapper.static ObjectfromJsonNode(com.fasterxml.jackson.databind.JsonNode jsonNode)static <T> TfromJsonNode(com.fasterxml.jackson.databind.JsonNode jsonNode, Class<? extends T> objClass)static com.fasterxml.jackson.databind.node.ArrayNodemakeArrayNode(Object[] arr)Creates an ArrayNode from an array of objects.static com.fasterxml.jackson.databind.node.ArrayNodemakeArrayNode(List<?> list)Creates an ArrayNode from a List of objects.static StringmakeJson(com.fasterxml.jackson.databind.JsonNode node)Converts a JsonNode to a JSON string.static StringmakeJson(String yaml)Converts a YAML string to a pretty-printed JSON string.static StringmakeJson(String fieldName, Object[] arr)Creates a JSON string with an array field from an array of objects.static StringmakeJson(String fieldName, String fieldValue)Creates a simple JSON string with a single field-value pair.static StringmakeJson(String fieldName, List<?> list)Creates a JSON string with an array field from a List of objects.static com.fasterxml.jackson.databind.JsonNodemakeNode(Object obj)Converts a Java object to a JsonNode.static com.fasterxml.jackson.databind.JsonNodemakeNode(String json)Creates a JsonNode from a JSON string.static com.fasterxml.jackson.databind.JsonNodemakeNode(String fieldName, Object[] arr)Creates a JSON object node with an array field containing the provided objects.static com.fasterxml.jackson.databind.JsonNodemakeNode(String fieldName, String fieldValue)Creates a simple JSON object node with a single string field.static com.fasterxml.jackson.databind.JsonNodemakeNode(String fieldName, List<?> list)Creates a JSON object node with an array field containing objects from a List.static com.fasterxml.jackson.databind.JsonNode[]makeNodes(Object[] arr)Creates an array of JsonNode objects from an array of Java objects.static com.fasterxml.jackson.databind.JsonNode[]makeNodes(List<?> list)Creates an array of JsonNode objects from a List of Java objects.static StringmakePrettyJson(Object obj)Converts an object to a pretty-printed JSON string.static StringtoJson(Object obj, boolean prettyPrint)Converts an object to JSON string with configurable pretty-printing.
-
-
-
Method Detail
-
makeNode
public static com.fasterxml.jackson.databind.JsonNode makeNode(String json)
Creates a JsonNode from a JSON string.- Parameters:
json- the JSON string to parse- Returns:
- JsonNode representing the parsed JSON, or null if parsing fails
-
makeNode
public static com.fasterxml.jackson.databind.JsonNode makeNode(Object obj)
Converts a Java object to a JsonNode.- Parameters:
obj- the Java object to convert- Returns:
- JsonNode representation of the object
-
makeNode
public static com.fasterxml.jackson.databind.JsonNode makeNode(String fieldName, Object[] arr)
Creates a JSON object node with an array field containing the provided objects.- Parameters:
fieldName- the name of the array fieldarr- the array of objects to include in the array field- Returns:
- ObjectNode containing the specified array field
-
makeArrayNode
public static com.fasterxml.jackson.databind.node.ArrayNode makeArrayNode(Object[] arr)
Creates an ArrayNode from an array of objects.- Parameters:
arr- the array of objects to convert- Returns:
- ArrayNode containing JsonNode representations of the objects
-
makeArrayNode
public static com.fasterxml.jackson.databind.node.ArrayNode makeArrayNode(List<?> list)
Creates an ArrayNode from a List of objects.- Parameters:
list- the List of objects to convert- Returns:
- ArrayNode containing JsonNode representations of the objects, or null if input is null
-
makeNodes
public static com.fasterxml.jackson.databind.JsonNode[] makeNodes(Object[] arr)
Creates an array of JsonNode objects from an array of Java objects.- Parameters:
arr- the array of objects to convert- Returns:
- array of JsonNode representations of the objects
-
makeNodes
public static com.fasterxml.jackson.databind.JsonNode[] makeNodes(List<?> list)
Creates an array of JsonNode objects from a List of Java objects.- Parameters:
list- the List of objects to convert- Returns:
- array of JsonNode representations of the objects, or null if input is null
-
makeNode
public static com.fasterxml.jackson.databind.JsonNode makeNode(String fieldName, List<?> list)
Creates a JSON object node with an array field containing objects from a List.- Parameters:
fieldName- the name of the array fieldlist- the List of objects to include in the array field- Returns:
- ObjectNode containing the specified array field, or null if input is null
-
makeNode
public static com.fasterxml.jackson.databind.JsonNode makeNode(String fieldName, String fieldValue)
Creates a simple JSON object node with a single string field.- Parameters:
fieldName- the name of the fieldfieldValue- the string value of the field- Returns:
- JsonNode representing the simple object
-
makeJson
public static String makeJson(String fieldName, String fieldValue)
Creates a simple JSON string with a single field-value pair.- Parameters:
fieldName- the name of the fieldfieldValue- the string value of the field- Returns:
- JSON string representation of the simple object
-
makeJson
public static String makeJson(String fieldName, Object[] arr)
Creates a JSON string with an array field from an array of objects.- Parameters:
fieldName- the name of the array fieldarr- the array of objects to include- Returns:
- JSON string representation of the object with array field
-
makeJson
public static String makeJson(String fieldName, List<?> list)
Creates a JSON string with an array field from a List of objects.- Parameters:
fieldName- the name of the array fieldlist- the List of objects to include- Returns:
- JSON string representation of the object with array field, or null if input is null
-
makeJson
public static String makeJson(com.fasterxml.jackson.databind.JsonNode node)
Converts a JsonNode to a JSON string.- Parameters:
node- the JsonNode to convert- Returns:
- JSON string representation, or null if conversion fails
-
makeJson
public static String makeJson(String yaml)
Converts a YAML string to a pretty-printed JSON string.- Parameters:
yaml- the YAML string to convert- Returns:
- pretty-printed JSON string representation, or empty string if conversion fails
-
makePrettyJson
public static String makePrettyJson(Object obj)
Converts an object to a pretty-printed JSON string.- Parameters:
obj- the object to convert- Returns:
- pretty-printed JSON string representation, or empty string if conversion fails
-
toJson
public static String toJson(Object obj, boolean prettyPrint)
Converts an object to JSON string with configurable pretty-printing.- Parameters:
obj- the object to convertprettyPrint- true to enable pretty-printing, false for compact format- Returns:
- JSON string representation, or empty string if conversion fails
-
fromJson
public static <T> T fromJson(String json, Class<? extends T> objClass)
Deserializes JSON string to an object of the specified class using the primary ObjectMapper.- Type Parameters:
T- the type of the object to deserialize- Parameters:
json- the JSON string to deserializeobjClass- the class of the object to deserialize- Returns:
- deserialized object, or null if deserialization fails
-
fromJson
public static <T> T fromJson(String json, com.fasterxml.jackson.core.type.TypeReference<T> type)
-
fromJson2
public static <T> T fromJson2(String json, Class<? extends T> objClass)
Deserializes JSON string to an object of the specified class using the alternative ObjectMapper. This version uses the ObjectId deserializer that expects direct string values.- Type Parameters:
T- the type of the object to deserialize- Parameters:
json- the JSON string to deserializeobjClass- the class of the object to deserialize- Returns:
- deserialized object, or null if deserialization fails
-
fromJsonNode
public static <T> T fromJsonNode(com.fasterxml.jackson.databind.JsonNode jsonNode, Class<? extends T> objClass)
-
fromJsonNode
public static Object fromJsonNode(com.fasterxml.jackson.databind.JsonNode jsonNode)
-
-