Class JSONWriter
java.lang.Object
org.springframework.extensions.webscripts.json.JSONWriter
Fast 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 -
Method Summary
Modifier and TypeMethodDescriptionstatic StringSafely encode a JSON string value.static StringencodeToJSON(Object obj) Encode a simple Java object structure to JSON text.static voidencodeToJSON(Object obj, JSONWriter writer) Encode a simple Java object structure to JSON text.endArray()End an array structure.End an object structure.endValue()End a value that was started with startValue()Start an array structure, the endArray() method must be called later.Start an object structure, the endObject() method must be called later.startValue(String name) Start a value (outputs just a name key), the endValue() method must be called later.Output a JSON null value.writeNullValue(String name) Output a JSON null value.writeRawValue(RawValue value) Output a JSON boolean value.writeValue(boolean value) Output a JSON boolean value.writeValue(double value) Output a JSON number value.writeValue(float value) Output a JSON number value.writeValue(int value) Output a JSON number value.writeValue(long value) Output a JSON number value.writeValue(String value) Output a JSON string value.writeValue(String name, boolean value) Output a JSON boolean name and value pair.writeValue(String name, double value) Output a JSON number name and value pair.writeValue(String name, float value) Output a JSON number name and value pair.writeValue(String name, int value) Output a JSON number name and value pair.writeValue(String name, long value) Output a JSON number name and value pair.writeValue(String name, String value) Output a JSON string name and value pair.
-
Constructor Details
-
JSONWriter
Constructor- Parameters:
out- The Writer to immediately append values to (no internal buffering)
-
-
Method Details
-
startArray
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:
IOException
-
endArray
End an array structure.- Throws:
IOException
-
startObject
Start an object structure, the endObject() method must be called later.- Throws:
IOException
-
endObject
End an object structure.- Throws:
IOException
-
startValue
Start a value (outputs just a name key), the endValue() method must be called later. NOTE: follow with an array or object only.- Throws:
IOException
-
endValue
End a value that was started with startValue() -
writeValue
Output a JSON string name and value pair.- Throws:
IOException
-
writeValue
Output a JSON number name and value pair.- Throws:
IOException
-
writeValue
Output a JSON number name and value pair.- Throws:
IOException
-
writeValue
Output a JSON number name and value pair.- Throws:
IOException
-
writeValue
Output a JSON number name and value pair.- Throws:
IOException
-
writeValue
Output a JSON boolean name and value pair.- Throws:
IOException
-
writeValue
Output a JSON string value. NOTE: no name is written - call from within an array structure.- Throws:
IOException
-
writeValue
Output a JSON number value. NOTE: no name is written - call from within an array structure.- Throws:
IOException
-
writeValue
Output a JSON number value. NOTE: no name is written - call from within an array structure.- Throws:
IOException
-
writeValue
Output a JSON number value. NOTE: no name is written - call from within an array structure.- Throws:
IOException
-
writeValue
Output a JSON number value. NOTE: no name is written - call from within an array structure.- Throws:
IOException
-
writeValue
Output a JSON boolean value. NOTE: no name is written - call from within an array structure.- Throws:
IOException
-
writeNullValue
Output a JSON null value. NOTE: no name is written - call from within an array structure.- Throws:
IOException
-
writeNullValue
Output a JSON null value.- Throws:
IOException
-
writeRawValue
Output a JSON boolean value. NOTE: no name is written - call from within an array structure.- Throws:
IOException
-
encodeToJSON
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
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.writer- JSONWriter for output- Throws:
IOException
-
encodeJSONString
Safely encode a JSON string value.- Returns:
- encoded string, null is handled and returned as "".
-