org.apache.pivot.json
Class JSONSerializer

java.lang.Object
  extended by org.apache.pivot.json.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.


Field Summary
static int BUFFER_SIZE
           
static String DEFAULT_CHARSET_NAME
           
static Type DEFAULT_TYPE
           
static String JSON_EXTENSION
           
static String MIME_TYPE
           
 
Constructor Summary
JSONSerializer()
           
JSONSerializer(Charset charset)
           
JSONSerializer(Charset charset, Type type)
           
JSONSerializer(Type type)
           
 
Method Summary
 boolean getAlwaysDelimitMapKeys()
          Returns a flag indicating whether or not map keys will always be quote-delimited.
 Charset getCharset()
          Returns the character set used to encode/decode the JSON data.
 ListenerList<JSONSerializerListener> getJSONSerializerListeners()
           
 String getMIMEType(Object object)
          Returns the MIME type of the data read and written by this serializer.
 Type getType()
          Returns the type of the object that will be returned by readObject(Reader).
 boolean isVerbose()
          Returns the serializer's verbosity flag.
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.
 Object readObject(InputStream inputStream)
          Reads data from a JSON stream.
 Object readObject(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 setVerbose(boolean verbose)
          Sets the serializer's verbosity flag.
static String toString(Object value)
          Converts a object to a JSON string representation.
static String toString(Object value, boolean alwaysDelimitMapKeys)
          Converts a object to a JSON string representation.
 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

DEFAULT_CHARSET_NAME

public static final String DEFAULT_CHARSET_NAME
See Also:
Constant Field Values

DEFAULT_TYPE

public static final Type DEFAULT_TYPE

JSON_EXTENSION

public static final String JSON_EXTENSION
See Also:
Constant Field Values

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(Charset charset)

JSONSerializer

public JSONSerializer(Type type)

JSONSerializer

public JSONSerializer(Charset charset,
                      Type type)
Method Detail

getCharset

public Charset getCharset()
Returns the character set used to encode/decode the JSON data.


getType

public Type getType()
Returns the type of the object that will be returned by readObject(Reader).


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.

isVerbose

public boolean isVerbose()
Returns the serializer's verbosity flag.


setVerbose

public void setVerbose(boolean verbose)
Sets the serializer's verbosity flag. When verbosity is enabled, all data read or written will be echoed to the console.

Parameters:
verbose -

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 and the value of getType():
  • pivot.collections.Dictionary
  • pivot.collections.Sequence
  • java.lang.String
  • java.lang.Number
  • java.lang.Boolean
  • null
  • A JavaBean object
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:
  • pivot.collections.Map
  • pivot.collections.List
  • java.lang.String
  • java.lang.Number
  • java.lang.Boolean
  • null
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.

parse

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

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

parseString

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

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

parseNumber

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

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

parseShort

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

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

parseInteger

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

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

parseLong

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

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

parseFloat

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

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

parseDouble

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

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

parseBoolean

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

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

parseList

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

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

parseMap

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

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

toString

public static String toString(Object value)
                       throws SerializationException
Converts a object to a JSON string representation. The map keys will always be quote-delimited.

Parameters:
value - The object to convert.
Returns:
The resulting JSON string.
Throws:
SerializationException
See Also:
toString(Object, boolean)

toString

public static String toString(Object value,
                              boolean alwaysDelimitMapKeys)
                       throws SerializationException
Converts a object to a JSON string representation.

Parameters:
value - The object to convert.
alwaysDelimitMapKeys - A flag indicating whether or not map keys will always be quote-delimited.
Returns:
The resulting JSON string.
Throws:
SerializationException

getJSONSerializerListeners

public ListenerList<JSONSerializerListener> getJSONSerializerListeners()