Class JsonUtils

java.lang.Object
com.dtsx.astra.sdk.utils.JsonUtils

public class JsonUtils extends Object
Custom implementation of serialization : faster + no jackson dependency
Author:
Cedrick Lunven (@clunven)
  • Method Details

    • escapeJson

      public 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. 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

      public static final String valueAsJson(Object value)
      Target primitive displayed as JSON.
      Parameters:
      value - object value
      Returns:
      target json expression
    • collectionAsJson

      public static final <T> String collectionAsJson(Collection<T> pCollec)
      Serialize a collection of object as Json. Element should eventually override toString() to produce JSON.
      Type Parameters:
      T - T
      Parameters:
      pCollec - input collection
      Returns:
      collection as String
    • mapAsJson

      public static final <K, V> String mapAsJson(Map<K,V> pMap)
      Serialize a map of objects as Json. Elements should override toString() to produce JSON.
      Type Parameters:
      K - K
      V - 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

      public static String marshall(Object o)
      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 String
      ref - type Reference to map the result
      Returns:
      expected object
    • unmarshallBean

      public static <T> T unmarshallBean(String body, Class<T> ref)
      Load body as expected object.
      Type Parameters:
      T - parameter
      Parameters:
      body - response body as String
      ref - type Reference to map the result
      Returns:
      expected objects