Package io.airlift.json
Class JsonCodec<T>
- java.lang.Object
-
- io.airlift.json.JsonCodec<T>
-
public class JsonCodec<T> extends Object
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description TfromJson(byte[] json)Coverts the specified json bytes (UTF-8) into an instance of type T.TfromJson(String json)Converts the specified json string into an instance of type T.TypegetType()Gets the type this codec supports.static <T> JsonCodec<T>jsonCodec(com.google.common.reflect.TypeToken<T> type)static <T> JsonCodec<T>jsonCodec(Class<T> type)static <T> JsonCodec<List<T>>listJsonCodec(JsonCodec<T> type)static <T> JsonCodec<List<T>>listJsonCodec(Class<T> type)static <K,V>
JsonCodec<Map<K,V>>mapJsonCodec(Class<K> keyType, JsonCodec<V> valueType)static <K,V>
JsonCodec<Map<K,V>>mapJsonCodec(Class<K> keyType, Class<V> valueType)StringtoJson(T instance)Converts the specified instance to json.byte[]toJsonBytes(T instance)Converts the specified instance to json.Optional<String>toJsonWithLengthLimit(T instance, int lengthLimit)Converts the specified instance to optional json string with a length limit.
-
-
-
Method Detail
-
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)
-
mapJsonCodec
public static <K,V> JsonCodec<Map<K,V>> mapJsonCodec(Class<K> keyType, JsonCodec<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 jsonlengthLimit- 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
-
-