Class JsonCodec<T>


  • public class JsonCodec<T>
    extends Object
    • Method Detail

      • jsonCodec

        public static <T> JsonCodec<T> jsonCodec​(Class<T> type)
      • jsonCodec

        public static <T> JsonCodec<T> jsonCodec​(com.google.common.reflect.TypeToken<T> type)
      • mapJsonCodec

        public static <K,​V> JsonCodec<Map<K,​V>> mapJsonCodec​(Class<K> keyType,
                                                                         Class<V> valueType)
      • getType

        public Type getType()
        Gets the type this codec supports.
      • fromJson

        public T fromJson​(String json)
                   throws IllegalArgumentException
        Converts the specified json string into an instance of type T.
        Parameters:
        json - the json string to parse
        Returns:
        parsed response; never null
        Throws:
        IllegalArgumentException - if the json string can not be converted to the type T
      • toJson

        public String toJson​(T instance)
                      throws IllegalArgumentException
        Converts the specified instance to json.
        Parameters:
        instance - the instance to convert to json
        Returns:
        json string
        Throws:
        IllegalArgumentException - if the specified instance can not be converted to json
      • toJsonWithLengthLimit

        public Optional<String> toJsonWithLengthLimit​(T instance,
                                                      int lengthLimit)
        Converts the specified instance to optional json string with a length limit. Returns Optional.empty() if length limit is exceeded.
        Parameters:
        instance - the instance to convert to json
        lengthLimit - the maximum length of the serialized string in characters
        Returns:
        json string
        Throws:
        IllegalArgumentException - if the specified instance can not be converted to json
      • fromJson

        public T fromJson​(byte[] json)
                   throws IllegalArgumentException
        Coverts the specified json bytes (UTF-8) into an instance of type T.
        Parameters:
        json - the json bytes (UTF-8) to parse
        Returns:
        parsed response; never null
        Throws:
        IllegalArgumentException - if the json bytes can not be converted to the type T
      • toJsonBytes

        public byte[] toJsonBytes​(T instance)
                           throws IllegalArgumentException
        Converts the specified instance to json.
        Parameters:
        instance - the instance to convert to json
        Returns:
        json bytes (UTF-8)
        Throws:
        IllegalArgumentException - if the specified instance can not be converted to json