pivot.serialization
Class JSONSerializer

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

public class JSONSerializer
extends Object
implements Serializer<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 String MIME_TYPE
           
 
Constructor Summary
JSONSerializer()
           
JSONSerializer(Charset charset)
           
JSONSerializer(String charsetName)
           
 
Method Summary
static boolean containsKey(Object root, String path)
          Tests the existence of a path in a given object.
static Object get(Object root, String path)
          Returns the value at the given path.
 boolean getAlwaysDelimitMapKeys()
          Returns a flag indicating whether or not map keys will always be quote-delimited.
static Boolean getBoolean(Object root, String path)
          Returns the value at the given path as a boolean.
static Double getDouble(Object root, String path)
          Returns the value at the given path as a double.
static Float getFloat(Object root, String path)
          Returns the value at the given path as a float.
static Integer getInteger(Object root, String path)
          Returns the value at the given path as an integer.
static List<?> getList(Object root, String path)
          Returns the value at the given path as a list.
static Long getLong(Object root, String path)
          Returns the value at the given path as a long.
static Map<String,?> getMap(Object root, String path)
          Returns the value at the given path as a map.
 String getMIMEType(Object object)
          Returns the MIME type of the data read and written by this serializer.
static Number getNumber(Object root, String path)
          Returns the value at the given path as a number.
static Short getShort(Object root, String path)
          Returns the value at the given path as a short.
static String getString(Object root, String path)
          Returns the value at the given path as a string.
static Object getValue(Object root, String path)
          Deprecated. This method is deprecated; use get(Object, String) instead.
static Object parse(String json)
          Converts a JSON value to a Java object.
static Boolean parseBoolean(String json)
          Converts a JSON value to a boolean.
static Double parseDouble(String json)
          Converts a JSON value to a double.
static Float parseFloat(String json)
          Converts a JSON value to a float.
static Integer parseInteger(String json)
          Converts a JSON value to a integer.
static List<?> parseList(String json)
          Converts a JSON value to a list.
static Long parseLong(String json)
          Converts a JSON value to a long.
static Map<String,?> parseMap(String json)
          Converts a JSON value to a map.
static Number parseNumber(String json)
          Converts a JSON value to a number.
static Short parseShort(String json)
          Converts a JSON value to a short.
static String parseString(String json)
          Converts a JSON value to a string.
static Object put(Object root, String path, Object value)
          Sets the value at the given path.
 Object readObject(InputStream inputStream)
          Reads data from a JSON stream.
 Object readObject(Reader reader)
          Reads data from a JSON stream.
static Object remove(Object root, String path)
          Removes the value at the given path.
 void setAlwaysDelimitMapKeys(boolean alwaysDelimitMapKeys)
          Sets a flag indicating that map keys should always be quote-delimited.
 void writeObject(Object object, OutputStream outputStream)
          Writes data to a JSON stream.
 void writeObject(Object object, 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 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(String charsetName)

JSONSerializer

public JSONSerializer(Charset charset)
Method Detail

readObject

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

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

readObject

public Object readObject(Reader reader)
                  throws 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:
IOException
SerializationException

writeObject

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

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

writeObject

public void writeObject(Object object,
                        Writer writer)
                 throws 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:
IOException
SerializationException

getMIMEType

public String getMIMEType(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<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

@Deprecated
public static Object getValue(Object root,
                                         String path)
Deprecated. This method is deprecated; use get(Object, String) instead.

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.

get

public static Object get(Object root,
                         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 String getString(Object root,
                               String path)
Returns the value at the given path as a string.

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

getNumber

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

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

getShort

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

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

getInteger

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

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

getLong

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

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

getFloat

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

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

getDouble

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

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

getBoolean

public static Boolean getBoolean(Object root,
                                 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(Object root,
                              String path)
Returns the value at the given path as a list.

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

getMap

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

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

put

public static Object put(Object root,
                         String path,
                         Object value)
Sets the value at the given path.

Parameters:
root -
path -
value -
Returns:
The value previously associated with the path.

remove

public static Object remove(Object root,
                            String path)
Removes the value at the given path.

Parameters:
root -
path -
Returns:
The value that was removed.

containsKey

public static boolean containsKey(Object root,
                                  String path)
Tests the existence of a path in a given object.

Parameters:
root -
path -
Returns:
true if the path exists; false, otherwise.

parse

public static Object parse(String json)
Converts a JSON value to a Java object.

Parameters:
json - The JSON value.
Returns:
The parsed object.

parseString

public static String parseString(String json)
Converts a JSON value to a string.

Parameters:
json - The JSON value.
Returns:
The parsed string.

parseNumber

public static Number parseNumber(String json)
Converts a JSON value to a number.

Parameters:
json - The JSON value.
Returns:
The parsed number.

parseShort

public static Short parseShort(String json)
Converts a JSON value to a short.

Parameters:
json - The JSON value.
Returns:
The parsed short.

parseInteger

public static Integer parseInteger(String json)
Converts a JSON value to a integer.

Parameters:
json - The JSON value.
Returns:
The parsed integer.

parseLong

public static Long parseLong(String json)
Converts a JSON value to a long.

Parameters:
json - The JSON value.
Returns:
The parsed number.

parseFloat

public static Float parseFloat(String json)
Converts a JSON value to a float.

Parameters:
json - The JSON value.
Returns:
The parsed float.

parseDouble

public static Double parseDouble(String json)
Converts a JSON value to a double.

Parameters:
json - The JSON value.
Returns:
The parsed double.

parseBoolean

public static Boolean parseBoolean(String json)
Converts a JSON value to a boolean.

Parameters:
json - The JSON value.
Returns:
The parsed boolean.

parseList

public static List<?> parseList(String json)
Converts a JSON value to a list.

Parameters:
json - The JSON value.
Returns:
The parsed list.

parseMap

public static Map<String,?> parseMap(String json)
Converts a JSON value to a map.

Parameters:
json - The JSON value.
Returns:
The parsed map.