Class JSONWriter
- java.lang.Object
-
- org.springframework.extensions.webscripts.json.JSONWriter
-
public final class JSONWriter extends java.lang.ObjectFast and simple JSON stream writer. Wraps a Writer to output a JSON object stream. No intermediate objects are created - writes are immediate to the underlying stream. Quoted and correct JSON encoding is performed on string values, - encoding is not performed on key names - it is assumed they are simple strings. The developer must call JSONWriter.encodeJSONString() on the key name if required.The JSON output is safe to be rendered directly into a "text/javascript" mimetype resource as all unicode characters that are not supported in JavaScript are encoded in hex \\uXXXX format.
- Since:
- 1.0 Added improvements to support 'double' and 'long' datatype and all methods now return the current JSONWriter to allow chaining of calls for more succinct serialization code., 1.2 Improvements to handle NaN/Infinity in double/float processing. Added helpers to recursively encode a hierarchy of Java POJO objects (List, Map, basic data-types) into a JSON string.
- Author:
- Kevin Roast
-
-
Constructor Summary
Constructors Constructor Description JSONWriter(java.io.Writer out)Constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.lang.StringencodeJSONString(java.lang.String s)Safely encode a JSON string value.static java.lang.StringencodeToJSON(java.lang.Object obj)Encode a simple Java object structure to JSON text.static voidencodeToJSON(java.lang.Object obj, JSONWriter writer)Encode a simple Java object structure to JSON text.JSONWriterendArray()End an array structure.JSONWriterendObject()End an object structure.JSONWriterendValue()End a value that was started with startValue()JSONWriterstartArray()Start an array structure, the endArray() method must be called later.JSONWriterstartObject()Start an object structure, the endObject() method must be called later.JSONWriterstartValue(java.lang.String name)Start a value (outputs just a name key), the endValue() method must be called later.JSONWriterwriteNullValue()Output a JSON null value.JSONWriterwriteNullValue(java.lang.String name)Output a JSON null value.JSONWriterwriteRawValue(RawValue value)Output a JSON boolean value.JSONWriterwriteValue(boolean value)Output a JSON boolean value.JSONWriterwriteValue(double value)Output a JSON number value.JSONWriterwriteValue(float value)Output a JSON number value.JSONWriterwriteValue(int value)Output a JSON number value.JSONWriterwriteValue(long value)Output a JSON number value.JSONWriterwriteValue(java.lang.String value)Output a JSON string value.JSONWriterwriteValue(java.lang.String name, boolean value)Output a JSON boolean name and value pair.JSONWriterwriteValue(java.lang.String name, double value)Output a JSON number name and value pair.JSONWriterwriteValue(java.lang.String name, float value)Output a JSON number name and value pair.JSONWriterwriteValue(java.lang.String name, int value)Output a JSON number name and value pair.JSONWriterwriteValue(java.lang.String name, long value)Output a JSON number name and value pair.JSONWriterwriteValue(java.lang.String name, java.lang.String value)Output a JSON string name and value pair.
-
-
-
Method Detail
-
startArray
public JSONWriter startArray() throws java.io.IOException
Start an array structure, the endArray() method must be called later. NOTE: Within the array, either output objects or use the single arg writeValue() method.- Throws:
java.io.IOException
-
endArray
public JSONWriter endArray() throws java.io.IOException
End an array structure.- Throws:
java.io.IOException
-
startObject
public JSONWriter startObject() throws java.io.IOException
Start an object structure, the endObject() method must be called later.- Throws:
java.io.IOException
-
endObject
public JSONWriter endObject() throws java.io.IOException
End an object structure.- Throws:
java.io.IOException
-
startValue
public JSONWriter startValue(java.lang.String name) throws java.io.IOException
Start a value (outputs just a name key), the endValue() method must be called later. NOTE: follow with an array or object only.- Throws:
java.io.IOException
-
endValue
public JSONWriter endValue()
End a value that was started with startValue()
-
writeValue
public JSONWriter writeValue(java.lang.String name, java.lang.String value) throws java.io.IOException
Output a JSON string name and value pair.- Throws:
java.io.IOException
-
writeValue
public JSONWriter writeValue(java.lang.String name, int value) throws java.io.IOException
Output a JSON number name and value pair.- Throws:
java.io.IOException
-
writeValue
public JSONWriter writeValue(java.lang.String name, long value) throws java.io.IOException
Output a JSON number name and value pair.- Throws:
java.io.IOException
-
writeValue
public JSONWriter writeValue(java.lang.String name, float value) throws java.io.IOException
Output a JSON number name and value pair.- Throws:
java.io.IOException
-
writeValue
public JSONWriter writeValue(java.lang.String name, double value) throws java.io.IOException
Output a JSON number name and value pair.- Throws:
java.io.IOException
-
writeValue
public JSONWriter writeValue(java.lang.String name, boolean value) throws java.io.IOException
Output a JSON boolean name and value pair.- Throws:
java.io.IOException
-
writeValue
public JSONWriter writeValue(java.lang.String value) throws java.io.IOException
Output a JSON string value. NOTE: no name is written - call from within an array structure.- Throws:
java.io.IOException
-
writeValue
public JSONWriter writeValue(int value) throws java.io.IOException
Output a JSON number value. NOTE: no name is written - call from within an array structure.- Throws:
java.io.IOException
-
writeValue
public JSONWriter writeValue(long value) throws java.io.IOException
Output a JSON number value. NOTE: no name is written - call from within an array structure.- Throws:
java.io.IOException
-
writeValue
public JSONWriter writeValue(float value) throws java.io.IOException
Output a JSON number value. NOTE: no name is written - call from within an array structure.- Throws:
java.io.IOException
-
writeValue
public JSONWriter writeValue(double value) throws java.io.IOException
Output a JSON number value. NOTE: no name is written - call from within an array structure.- Throws:
java.io.IOException
-
writeValue
public JSONWriter writeValue(boolean value) throws java.io.IOException
Output a JSON boolean value. NOTE: no name is written - call from within an array structure.- Throws:
java.io.IOException
-
writeNullValue
public JSONWriter writeNullValue() throws java.io.IOException
Output a JSON null value. NOTE: no name is written - call from within an array structure.- Throws:
java.io.IOException
-
writeNullValue
public JSONWriter writeNullValue(java.lang.String name) throws java.io.IOException
Output a JSON null value.- Throws:
java.io.IOException
-
writeRawValue
public JSONWriter writeRawValue(RawValue value) throws java.io.IOException
Output a JSON boolean value. NOTE: no name is written - call from within an array structure.- Throws:
java.io.IOException
-
encodeToJSON
public static java.lang.String encodeToJSON(java.lang.Object obj)
Encode a simple Java object structure to JSON text.Handles standard Java data types such as String, Boolean, Integer, Float, Double, null. Also deals with simple List as JSON Array and Map as JSON Object. Recursively processes lists and maps as needed.
- Parameters:
obj- Java object of basic data types or List or Map.- Returns:
- JSON string.
-
encodeToJSON
public static void encodeToJSON(java.lang.Object obj, JSONWriter writer) throws java.io.IOExceptionEncode a simple Java object structure to JSON text.Handles standard Java data types such as String, Boolean, Integer, Float, Double, null. Also deals with simple List as JSON Array and Map as JSON Object. Recursively processes lists and maps as needed.
- Parameters:
obj- Java object of basic data types or List or Map.writer- JSONWriter for output- Throws:
java.io.IOException
-
encodeJSONString
public static java.lang.String encodeJSONString(java.lang.String s)
Safely encode a JSON string value.- Returns:
- encoded string, null is handled and returned as "".
-
-