Package com.dtsx.astra.sdk.utils
Class JsonUtils
java.lang.Object
com.dtsx.astra.sdk.utils.JsonUtils
Custom implementation of serialization : faster + no jackson dependency
- Author:
- Cedrick Lunven (@clunven)
-
Method Summary
Modifier and TypeMethodDescriptionstatic final <T> String
collectionAsJson
(Collection<T> pCollec) Serialize a collection of object as Json.static final String
escapeJson
(String value) Code is built based on https://github.com/fangyidong/json-simple/blob/master/src/main/java/org/json/simple/JSONValue.java by FangYidong fangyidong@yahoo.com.cn THANK YOU ! ff4j core needs to stay with no dependency.static com.fasterxml.jackson.databind.ObjectMapper
Access the singletong ObjectMapper.static final <K,
V> String Serialize a map of objects as Json.static String
Transform object as a String.static <T> T
unmarshallBean
(String body, Class<T> ref) Load body as expected object.static <T> T
unmarshallType
(String body, com.fasterxml.jackson.core.type.TypeReference<T> ref) Load body as expected object.static final String
valueAsJson
(Object value) Target primitive displayed as JSON.
-
Method Details
-
escapeJson
Code is built based on https://github.com/fangyidong/json-simple/blob/master/src/main/java/org/json/simple/JSONValue.java by FangYidong fangyidong@yahoo.com.cn THANK YOU ! ff4j core needs to stay with no dependency. The following characters are reserved characters and can not be used in JSON and must be properly escaped to be used in strings. - Backspace to be replaced with \b - Form feed to be replaced with \f - Newline to be replaced with \n - Carriage return to be replaced with \r - Tab to be replaced with \t - Double quote to be replaced with \" - Backslash to be replaced with \\- Parameters:
value
- string to be escaped- Returns:
- escaped JSON
-
valueAsJson
Target primitive displayed as JSON.- Parameters:
value
- object value- Returns:
- target json expression
-
collectionAsJson
Serialize a collection of object as Json. Element should eventually overridetoString()
to produce JSON.- Type Parameters:
T
- T- Parameters:
pCollec
- input collection- Returns:
- collection as String
-
mapAsJson
Serialize a map of objects as Json. Elements should overridetoString()
to produce JSON.- Type Parameters:
K
- KV
- V- Parameters:
pMap
- target properties- Returns:
- target json expression
-
getObjectMapper
public static com.fasterxml.jackson.databind.ObjectMapper getObjectMapper()Access the singletong ObjectMapper.- Returns:
- object mapper
-
marshall
Transform object as a String.- Parameters:
o
- object to be serialized.- Returns:
- body as String
-
unmarshallType
public static <T> T unmarshallType(String body, com.fasterxml.jackson.core.type.TypeReference<T> ref) Load body as expected object.- Type Parameters:
T
- parameter- Parameters:
body
- response body as Stringref
- type Reference to map the result- Returns:
- expected object
-
unmarshallBean
Load body as expected object.- Type Parameters:
T
- parameter- Parameters:
body
- response body as Stringref
- type Reference to map the result- Returns:
- expected objects
-