pivot.serialization
Class JSONSerializer

java.lang.Object
  extended by pivot.serialization.JSONSerializer
All Implemented Interfaces:
Serializer<java.lang.Object>

public class JSONSerializer
extends java.lang.Object
implements Serializer<java.lang.Object>

Implementation of the Serializer interface that reads data from and writes data to a JavaScript Object Notation (JSON) file.

TODO Wrap reader in a CountingReader that tracks line/character index.

Author:
gbrown

Field Summary
static int BUFFER_SIZE
           
static java.lang.String MIME_TYPE
           
 
Constructor Summary
JSONSerializer()
           
JSONSerializer(java.nio.charset.Charset charset)
           
JSONSerializer(java.lang.String charsetName)
           
 
Method Summary
 boolean getAlwaysDelimitMapKeys()
          Returns a flag indicating whether or not map keys will always be quote-delimited.
static java.lang.Boolean getBoolean(java.lang.Object root, java.lang.String path)
          Returns the value at the given path as a boolean.
static java.lang.Double getDouble(java.lang.Object root, java.lang.String path)
          Returns the value at the given path as a double.
static java.lang.Float getFloat(java.lang.Object root, java.lang.String path)
          Returns the value at the given path as a float.
static java.lang.Integer getInteger(java.lang.Object root, java.lang.String path)
          Returns the value at the given path as an integer.
static List<?> getList(java.lang.Object root, java.lang.String path)
          Returns the value at the given path as a list.
static java.lang.Long getLong(java.lang.Object root, java.lang.String path)
          Returns the value at the given path as a long.
static Map<java.lang.String,?> getMap(java.lang.Object root, java.lang.String path)
          Returns the value at the given path as a map.
 java.lang.String getMIMEType(java.lang.Object object)
          Returns the MIME type of the data read and written by this serializer.
static java.lang.Number getNumber(java.lang.Object root, java.lang.String path)
          Returns the value at the given path as a number.
static java.lang.Short getShort(java.lang.Object root, java.lang.String path)
          Returns the value at the given path as a short.
static java.lang.String getString(java.lang.Object root, java.lang.String path)
          Returns the value at the given path as a string.
static java.lang.Object getValue(java.lang.Object root, java.lang.String path)
          Returns the value at the given path.
static List<?> parseList(java.lang.String string)
          Parses a JSON-formatted array value into a list.
static Map<java.lang.String,?> parseMap(java.lang.String string)
          Parses a JSON-formatted object value into a map.
 java.lang.Object readObject(java.io.InputStream inputStream)
          Reads data from a JSON stream.
 java.lang.Object readObject(java.io.Reader reader)
          Reads data from a JSON stream.
 void setAlwaysDelimitMapKeys(boolean alwaysDelimitMapKeys)
          Sets a flag indicating that map keys should always be quote-delimited.
 void writeObject(java.lang.Object object, java.io.OutputStream outputStream)
          Writes data to a JSON stream.
 void writeObject(java.lang.Object object, java.io.Writer writer)
          Writes data to a JSON stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIME_TYPE

public static final java.lang.String MIME_TYPE
See Also:
Constant Field Values

BUFFER_SIZE

public static final int BUFFER_SIZE
See Also:
Constant Field Values
Constructor Detail

JSONSerializer

public JSONSerializer()

JSONSerializer

public JSONSerializer(java.lang.String charsetName)

JSONSerializer

public JSONSerializer(java.nio.charset.Charset charset)
Method Detail

readObject

public java.lang.Object readObject(java.io.InputStream inputStream)
                            throws java.io.IOException,
                                   SerializationException
Reads data from a JSON stream.

Specified by:
readObject in interface Serializer<java.lang.Object>
Parameters:
inputStream - The input stream from which data will be read.
Returns:
The deserialized object.
Throws:
java.io.IOException
SerializationException
See Also:
readObject(Reader)

readObject

public java.lang.Object readObject(java.io.Reader reader)
                            throws java.io.IOException,
                                   SerializationException
Reads data from a JSON stream.

Parameters:
reader - The reader from which data will be read.
Returns:
One of the following types, depending on the content of the stream:
  • java.lang.String
  • java.lang.Number
  • java.lang.Boolean
  • pivot.collections.List
  • pivot.collections.Map
Throws:
java.io.IOException
SerializationException

writeObject

public void writeObject(java.lang.Object object,
                        java.io.OutputStream outputStream)
                 throws java.io.IOException,
                        SerializationException
Writes data to a JSON stream.

Specified by:
writeObject in interface Serializer<java.lang.Object>
Parameters:
object -
outputStream - The output stream to which data will be written.
Throws:
java.io.IOException
SerializationException
See Also:
writeObject(Object, Writer)

writeObject

public void writeObject(java.lang.Object object,
                        java.io.Writer writer)
                 throws java.io.IOException,
                        SerializationException
Writes data to a JSON stream.

Parameters:
object - The object to serialize. Must be one of the following types:
  • java.lang.String
  • java.lang.Number
  • java.lang.Boolean
  • pivot.collections.List
  • pivot.collections.Map
writer - The writer to which data will be written.
Throws:
java.io.IOException
SerializationException

getMIMEType

public java.lang.String getMIMEType(java.lang.Object object)
Description copied from interface: Serializer
Returns the MIME type of the data read and written by this serializer.

Specified by:
getMIMEType in interface Serializer<java.lang.Object>
Parameters:
object - If provided, allows the serializer to attach parameters to the returned MIME type containing more detailed information about the data. If null, the base MIME type is returned.

getAlwaysDelimitMapKeys

public boolean getAlwaysDelimitMapKeys()
Returns a flag indicating whether or not map keys will always be quote-delimited.


setAlwaysDelimitMapKeys

public void setAlwaysDelimitMapKeys(boolean alwaysDelimitMapKeys)
Sets a flag indicating that map keys should always be quote-delimited.

Parameters:
alwaysDelimitMapKeys - true to bound map keys in double quotes; false to only quote-delimit keys as necessary.

getValue

public static java.lang.Object getValue(java.lang.Object root,
                                        java.lang.String path)
Returns the value at the given path.

Parameters:
root - The root object; must be an instance of Map or List.
path - The path to the value, in JavaScript path notation.
Returns:
The value at the given path.

getString

public static java.lang.String getString(java.lang.Object root,
                                         java.lang.String path)
Returns the value at the given path as a string.

Parameters:
root -
path -
See Also:
getValue(Object, String)

getNumber

public static java.lang.Number getNumber(java.lang.Object root,
                                         java.lang.String path)
Returns the value at the given path as a number.

Parameters:
root -
path -
See Also:
getValue(Object, String)

getShort

public static java.lang.Short getShort(java.lang.Object root,
                                       java.lang.String path)
Returns the value at the given path as a short.

Parameters:
root -
path -
See Also:
getValue(Object, String)

getInteger

public static java.lang.Integer getInteger(java.lang.Object root,
                                           java.lang.String path)
Returns the value at the given path as an integer.

Parameters:
root -
path -
See Also:
getValue(Object, String)

getLong

public static java.lang.Long getLong(java.lang.Object root,
                                     java.lang.String path)
Returns the value at the given path as a long.

Parameters:
root -
path -
See Also:
getValue(Object, String)

getFloat

public static java.lang.Float getFloat(java.lang.Object root,
                                       java.lang.String path)
Returns the value at the given path as a float.

Parameters:
root -
path -
See Also:
getValue(Object, String)

getDouble

public static java.lang.Double getDouble(java.lang.Object root,
                                         java.lang.String path)
Returns the value at the given path as a double.

Parameters:
root -
path -
See Also:
getValue(Object, String)

getBoolean

public static java.lang.Boolean getBoolean(java.lang.Object root,
                                           java.lang.String path)
Returns the value at the given path as a boolean.

Parameters:
root -
path -
See Also:
getValue(Object, String)

getList

public static List<?> getList(java.lang.Object root,
                              java.lang.String path)
Returns the value at the given path as a list.

Parameters:
root -
path -
See Also:
getValue(Object, String)

getMap

public static Map<java.lang.String,?> getMap(java.lang.Object root,
                                             java.lang.String path)
Returns the value at the given path as a map.

Parameters:
root -
path -
See Also:
getValue(Object, String)

parseList

public static List<?> parseList(java.lang.String string)
Parses a JSON-formatted array value into a list.

Parameters:
string - A string containing a JSON array (e.g. "[1, 2, 3]").
Returns:
A List instance containing the parsed JSON data.

parseMap

public static Map<java.lang.String,?> parseMap(java.lang.String string)
Parses a JSON-formatted object value into a map.

Parameters:
string - A string containing a JSON object (e.g. "{a:1, b:2, c:3}").
Returns:
A Map instance containing the parsed JSON data.