com.twitter.common.io
Class JsonCodec<T>

java.lang.Object
  extended by com.twitter.common.io.JsonCodec<T>
All Implemented Interfaces:
Codec<T>

public class JsonCodec<T>
extends Object
implements Codec<T>

A Codec that can encode and decode objects to and from JSON using the GSON library (which in turn will use reflection). The codec uses the UTF-8 encoding.


Method Summary
static
<T> JsonCodec<T>
create(Class<T> clazz)
          Creates a new JSON codec instance for objects of the specified class.
static
<T> JsonCodec<T>
create(Class<T> clazz, com.google.gson.Gson gson)
          Creates a new JSON codec instance for objects of the specified class and the specified Gson instance.
 T deserialize(InputStream source)
          Reads an item from the source stream that was written by Codec.serialize(Object, java.io.OutputStream).
static com.google.gson.ExclusionStrategy getThriftExclusionStrategy()
          Returns a Gson exclusion strategy that excludes Thrift synthetic fields from JSON serialization.
 void serialize(T item, OutputStream sink)
          Writes a representation of item to the sink that can be read back by Codec.deserialize(java.io.InputStream).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

create

public static <T> JsonCodec<T> create(Class<T> clazz)
Creates a new JSON codec instance for objects of the specified class.

Parameters:
clazz - the class of the objects the created codec is for.
Returns:
a newly constructed JSON codec instance for objects of the requested class.

create

public static <T> JsonCodec<T> create(Class<T> clazz,
                                      com.google.gson.Gson gson)
Creates a new JSON codec instance for objects of the specified class and the specified Gson instance. You can use this method if you need to customize the behavior of the Gson serializer.

Parameters:
clazz - the class of the objects the created codec is for.
gson - the Gson instance to use for serialization/deserialization.
Returns:
a newly constructed JSON codec instance for objects of the requested class.

getThriftExclusionStrategy

public static com.google.gson.ExclusionStrategy getThriftExclusionStrategy()
Returns a Gson exclusion strategy that excludes Thrift synthetic fields from JSON serialization. You can pass it to a GsonBuilder to construct a customized Gson instance to use with create(Class, Gson).

Returns:
a Gson exclusion strategy for thrift synthetic fields.

deserialize

public T deserialize(InputStream source)
              throws IOException
Description copied from interface: Codec
Reads an item from the source stream that was written by Codec.serialize(Object, java.io.OutputStream).

Specified by:
deserialize in interface Codec<T>
Parameters:
source - the stream to read an item from
Returns:
the deserialized item
Throws:
IOException - if there is a problem reading an item

serialize

public void serialize(T item,
                      OutputStream sink)
               throws IOException
Description copied from interface: Codec
Writes a representation of item to the sink that can be read back by Codec.deserialize(java.io.InputStream).

Specified by:
serialize in interface Codec<T>
Parameters:
item - the item to serialize
sink - the stream to write the item out to
Throws:
IOException - if there is a problem serializing the item