Package org.apache.pivot.json
Class JSONSerializer
- java.lang.Object
-
- org.apache.pivot.json.JSONSerializer
-
- All Implemented Interfaces:
Serializer<java.lang.Object>
public class JSONSerializer extends java.lang.Object implements Serializer<java.lang.Object>
Implementation of theSerializerinterface that reads data from and writes data to a JavaScript Object Notation (JSON) file.
-
-
Field Summary
Fields Modifier and Type Field Description static java.nio.charset.CharsetDEFAULT_CHARSETstatic java.lang.reflect.TypeDEFAULT_TYPEstatic java.lang.StringJSON_EXTENSIONstatic java.lang.StringMIME_TYPE
-
Constructor Summary
Constructors Constructor Description JSONSerializer()JSONSerializer(java.lang.reflect.Type objType)JSONSerializer(java.nio.charset.Charset cs)JSONSerializer(java.nio.charset.Charset cs, java.lang.reflect.Type objType)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleangetAllowMacros()Returns the flag indicating whether this serializer will allow macros in the text.booleangetAlwaysDelimitMapKeys()Returns a flag indicating whether or not map keys will always be quote-delimited.java.nio.charset.CharsetgetCharset()Returns the character set used to encode/decode the JSON data.ListenerList<JSONSerializerListener>getJSONSerializerListeners()java.lang.StringgetMIMEType(java.lang.Object object)Returns the MIME type of the data read and written by this serializer.java.lang.reflect.TypegetType()Returns the type of the object that will be returned byreadObject(Reader).booleanisVerbose()Returns the serializer's verbosity flag.static java.lang.Objectparse(java.lang.String json)Converts a JSON value to a Java object.static java.lang.BooleanparseBoolean(java.lang.String json)Converts a JSON value to a boolean.static java.lang.DoubleparseDouble(java.lang.String json)Converts a JSON value to a double.static java.lang.FloatparseFloat(java.lang.String json)Converts a JSON value to a float.static java.lang.IntegerparseInteger(java.lang.String json)Converts a JSON value to a integer.static List<?>parseList(java.lang.String json)Converts a JSON value to a list.static java.lang.LongparseLong(java.lang.String json)Converts a JSON value to a long.static Map<java.lang.String,?>parseMap(java.lang.String json)Converts a JSON value to a map.static java.lang.NumberparseNumber(java.lang.String json)Converts a JSON value to a number.static java.lang.ShortparseShort(java.lang.String json)Converts a JSON value to a short.static java.lang.StringparseString(java.lang.String json)Converts a JSON value to a string.java.lang.ObjectreadObject(java.io.InputStream inputStream)Reads data from a JSON stream.java.lang.ObjectreadObject(java.io.Reader reader)Reads data from a JSON stream.voidsetAllowMacros(boolean allowMacros)Sets the flag indicating whether macros are allowed in the text.voidsetAlwaysDelimitMapKeys(boolean delimitKeys)Sets a flag indicating that map keys should always be quote-delimited.voidsetVerbose(boolean verboseValue)Sets the serializer's verbosity flag.static java.lang.StringtoString(java.lang.Object value)Converts a object to a JSON string representation.static java.lang.StringtoString(java.lang.Object value, boolean alwaysDelimitMapKeys)Converts a object to a JSON string representation.voidwriteObject(java.lang.Object object, java.io.OutputStream outputStream)Writes data to a JSON stream.voidwriteObject(java.lang.Object object, java.io.Writer writer)Writes data to a JSON stream.
-
-
-
Field Detail
-
DEFAULT_CHARSET
public static final java.nio.charset.Charset DEFAULT_CHARSET
-
DEFAULT_TYPE
public static final java.lang.reflect.Type DEFAULT_TYPE
-
JSON_EXTENSION
public static final java.lang.String JSON_EXTENSION
- See Also:
- Constant Field Values
-
MIME_TYPE
public static final java.lang.String MIME_TYPE
- See Also:
- Constant Field Values
-
-
Method Detail
-
getCharset
public java.nio.charset.Charset getCharset()
Returns the character set used to encode/decode the JSON data.- Returns:
- The current character set.
-
getType
public java.lang.reflect.Type getType()
Returns the type of the object that will be returned byreadObject(Reader).- Returns:
- The current object type.
-
getAlwaysDelimitMapKeys
public boolean getAlwaysDelimitMapKeys()
Returns a flag indicating whether or not map keys will always be quote-delimited.Note: the JSON "standard" requires keys to be delimited.
- Returns:
trueif map keys must always be delimited (that is, enclosed in double quotes),falsefor the default behavior that does not require double quotes.
-
setAlwaysDelimitMapKeys
public void setAlwaysDelimitMapKeys(boolean delimitKeys)
Sets a flag indicating that map keys should always be quote-delimited.- Parameters:
delimitKeys-trueto bound map keys in double quotes;falseto only quote-delimit keys as necessary.
-
isVerbose
public boolean isVerbose()
Returns the serializer's verbosity flag.- Returns:
- The verbosity flag for this serializer.
-
setVerbose
public void setVerbose(boolean verboseValue)
Sets the serializer's verbosity flag. When verbosity is enabled, all data read or written will be echoed to the console.- Parameters:
verboseValue-trueto set verbose mode,falseto disable.
-
getAllowMacros
public boolean getAllowMacros()
Returns the flag indicating whether this serializer will allow macros in the text.- Returns:
- The "macros allowed" flag.
-
setAllowMacros
public void setAllowMacros(boolean allowMacros)
Sets the flag indicating whether macros are allowed in the text. This is definitely a non-standard feature. See the documentation inMacroReaderfor more details on the specification of macros.Note: must be called before
readObject(java.io.InputStream)is called.- Parameters:
allowMacros- Flag indicating whether macros are allowed (default isfalse). The flag must be set to true in order to activate this feature, because there is a definitely measured 25x slowdown when using it, even if no macros are defined.
-
readObject
public java.lang.Object readObject(java.io.InputStream inputStream) throws java.io.IOException, SerializationExceptionReads data from a JSON stream.- Specified by:
readObjectin interfaceSerializer<java.lang.Object>- Parameters:
inputStream- The input stream from which data will be read.- Returns:
- The deserialized object.
- Throws:
java.io.IOException- for any errors accessing or reading the object.SerializationException- for any formatting errors encountered while deserializing the object.- See Also:
readObject(Reader)
-
readObject
public java.lang.Object readObject(java.io.Reader reader) throws java.io.IOException, SerializationExceptionReads data from a JSON stream.Processes macros at this level using
MacroReader.- 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:
java.io.IOException- for any I/O related errors while reading.SerializationException- for any formatting errors in the data.
-
writeObject
public void writeObject(java.lang.Object object, java.io.OutputStream outputStream) throws java.io.IOException, SerializationExceptionWrites data to a JSON stream.- Specified by:
writeObjectin interfaceSerializer<java.lang.Object>- Parameters:
object- The root object to be written.outputStream- The output stream to which data will be written.- Throws:
java.io.IOException- for any errors during the writing process.SerializationException- for any formatting errors in the data.- See Also:
writeObject(Object, Writer)
-
writeObject
public void writeObject(java.lang.Object object, java.io.Writer writer) throws java.io.IOException, SerializationExceptionWrites 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:
java.io.IOException- for any errors during the writing process.SerializationException- for any formatting errors in the data.
-
getMIMEType
public java.lang.String getMIMEType(java.lang.Object object)
Description copied from interface:SerializerReturns the MIME type of the data read and written by this serializer.- Specified by:
getMIMETypein interfaceSerializer<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. Ifnull, the base MIME type is returned.- Returns:
- The MIME type of the current data.
-
parse
public static java.lang.Object parse(java.lang.String json) throws SerializationExceptionConverts a JSON value to a Java object.- Parameters:
json- The JSON value.- Returns:
- The parsed object.
- Throws:
SerializationException- for any formatting errors in the data.
-
parseString
public static java.lang.String parseString(java.lang.String json) throws SerializationExceptionConverts a JSON value to a string.- Parameters:
json- The JSON value.- Returns:
- The parsed string.
- Throws:
SerializationException- for any formatting errors in the data.
-
parseNumber
public static java.lang.Number parseNumber(java.lang.String json) throws SerializationExceptionConverts a JSON value to a number.- Parameters:
json- The JSON value.- Returns:
- The parsed number.
- Throws:
SerializationException- for any formatting errors in the data.
-
parseShort
public static java.lang.Short parseShort(java.lang.String json) throws SerializationExceptionConverts a JSON value to a short.- Parameters:
json- The JSON value.- Returns:
- The parsed short.
- Throws:
SerializationException- for any formatting errors in the data.
-
parseInteger
public static java.lang.Integer parseInteger(java.lang.String json) throws SerializationExceptionConverts a JSON value to a integer.- Parameters:
json- The JSON value.- Returns:
- The parsed integer.
- Throws:
SerializationException- for any formatting errors in the data.
-
parseLong
public static java.lang.Long parseLong(java.lang.String json) throws SerializationExceptionConverts a JSON value to a long.- Parameters:
json- The JSON value.- Returns:
- The parsed number.
- Throws:
SerializationException- for any formatting errors in the data.
-
parseFloat
public static java.lang.Float parseFloat(java.lang.String json) throws SerializationExceptionConverts a JSON value to a float.- Parameters:
json- The JSON value.- Returns:
- The parsed float.
- Throws:
SerializationException- for any formatting errors in the data.
-
parseDouble
public static java.lang.Double parseDouble(java.lang.String json) throws SerializationExceptionConverts a JSON value to a double.- Parameters:
json- The JSON value.- Returns:
- The parsed double.
- Throws:
SerializationException- for any formatting errors in the data.
-
parseBoolean
public static java.lang.Boolean parseBoolean(java.lang.String json) throws SerializationExceptionConverts a JSON value to a boolean.- Parameters:
json- The JSON value.- Returns:
- The parsed boolean.
- Throws:
SerializationException- for any formatting errors in the data.
-
parseList
public static List<?> parseList(java.lang.String json) throws SerializationException
Converts a JSON value to a list.- Parameters:
json- The JSON value.- Returns:
- The parsed list.
- Throws:
SerializationException- for any formatting errors in the data.
-
parseMap
public static Map<java.lang.String,?> parseMap(java.lang.String json) throws SerializationException
Converts a JSON value to a map.- Parameters:
json- The JSON value.- Returns:
- The parsed map.
- Throws:
SerializationException- for any formatting errors in the data.
-
toString
public static java.lang.String toString(java.lang.Object value) throws SerializationExceptionConverts 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- for any formatting errors in the data.- See Also:
toString(Object, boolean)
-
toString
public static java.lang.String toString(java.lang.Object value, boolean alwaysDelimitMapKeys) throws SerializationExceptionConverts 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- for any formatting errors in the data.
-
getJSONSerializerListeners
public ListenerList<JSONSerializerListener> getJSONSerializerListeners()
-
-