Class JsonUtils

java.lang.Object
io.stargate.sdk.utils.JsonUtils

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

    • getDataApiObjectMapper

      public static com.fasterxml.jackson.databind.ObjectMapper getDataApiObjectMapper()
      Building the data api specific object mapper.
      Returns:
      object mapper.
    • escapeJson

      public static 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 String valueAsJson(Object value)
      Target primitive displayed as JSON.
      Parameters:
      value - object value
      Returns:
      target json expression
    • collectionAsJson

      public static <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 <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
    • marshallForDataApi

      public static String marshallForDataApi(Object o)
      Transform object as a String.
      Parameters:
      o - object to be serialized.
      Returns:
      body as String
    • convertValue

      public static <T> T convertValue(Object bean, Class<T> clazz)
      Jackson deserialization.
      Type Parameters:
      T - current type
      Parameters:
      bean - current beam
      clazz - target class
      Returns:
      serialized
    • convertValueForDataApi

      public static <T> T convertValueForDataApi(Object bean, Class<T> clazz)
      Jackson deserialization.
      Type Parameters:
      T - current type
      Parameters:
      bean - current beam
      clazz - target class
      Returns:
      serialized
    • 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
    • unmarshallTypeForDataApi

      public static <T> T unmarshallTypeForDataApi(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
    • unmarshallBeanForDataApi

      public static <T> T unmarshallBeanForDataApi(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