com.rapportive.storm.serializer
Class SimpleJSONSerializer

java.lang.Object
  extended by com.rapportive.storm.serializer.SimpleJSONSerializer
All Implemented Interfaces:
backtype.storm.serialization.ISerialization<Object>

public class SimpleJSONSerializer
extends Object
implements backtype.storm.serialization.ISerialization<Object>

Serialisation scheme for JSON values using the json-simple library. Assumes the deserialised form will be a UTF-8-encoded string, and serialises into that form. N.B. currently this only supports JSON objects and arrays at the top level (though nested values are fine). N.B. if passed invalid JSON it will throw an IllegalArgumentException.

Author:
Sam Stokes (sam@rapportive.com)
See Also:
json-simple

Field Summary
static String ENCODING
          Encoding used to serialise and deserialise between byte streams and JSON structures.
 
Constructor Summary
SimpleJSONSerializer()
           
 
Method Summary
 boolean accept(Class c)
          Returns whether this serialisation can handle the given type.
 Object deserialize(DataInputStream stream)
          Deserialise a JSON value from the stream.
 void serialize(Object object, DataOutputStream stream)
          Serialise a JSON object or array to the stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENCODING

public static final String ENCODING
Encoding used to serialise and deserialise between byte streams and JSON structures. This is hard-coded to UTF-8 because the mechanism for registering a serialisation does not allow passing arguments to the serialisation constructor.

See Also:
Constant Field Values
Constructor Detail

SimpleJSONSerializer

public SimpleJSONSerializer()
Method Detail

accept

public boolean accept(Class c)
Returns whether this serialisation can handle the given type.

Specified by:
accept in interface backtype.storm.serialization.ISerialization<Object>
Returns:
true if c is JSONObject or JSONArray.

serialize

public void serialize(Object object,
                      DataOutputStream stream)
               throws IOException
Serialise a JSON object or array to the stream.

Specified by:
serialize in interface backtype.storm.serialization.ISerialization<Object>
Throws:
IllegalArgumentException - if object is not a JSON object or array
IOException - if there is an error writing to the stream.

deserialize

public Object deserialize(DataInputStream stream)
                   throws IOException
Deserialise a JSON value from the stream.

Specified by:
deserialize in interface backtype.storm.serialization.ISerialization<Object>
Throws:
IllegalArgumentException - if unable to parse a JSON value from the stream
IOException


Copyright © 2011. All Rights Reserved.