java.lang.Object
org.springframework.extensions.webscripts.json.JSONWriter

public final class JSONWriter extends Object
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 Details

    • JSONWriter

      public JSONWriter(Writer out)
      Constructor
      Parameters:
      out - The Writer to immediately append values to (no internal buffering)
  • Method Details

    • startArray

      public JSONWriter startArray() throws 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:
      IOException
    • endArray

      public JSONWriter endArray() throws IOException
      End an array structure.
      Throws:
      IOException
    • startObject

      public JSONWriter startObject() throws IOException
      Start an object structure, the endObject() method must be called later.
      Throws:
      IOException
    • endObject

      public JSONWriter endObject() throws IOException
      End an object structure.
      Throws:
      IOException
    • startValue

      public JSONWriter startValue(String name) throws 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:
      IOException
    • endValue

      public JSONWriter endValue()
      End a value that was started with startValue()
    • writeValue

      public JSONWriter writeValue(String name, String value) throws IOException
      Output a JSON string name and value pair.
      Throws:
      IOException
    • writeValue

      public JSONWriter writeValue(String name, int value) throws IOException
      Output a JSON number name and value pair.
      Throws:
      IOException
    • writeValue

      public JSONWriter writeValue(String name, long value) throws IOException
      Output a JSON number name and value pair.
      Throws:
      IOException
    • writeValue

      public JSONWriter writeValue(String name, float value) throws IOException
      Output a JSON number name and value pair.
      Throws:
      IOException
    • writeValue

      public JSONWriter writeValue(String name, double value) throws IOException
      Output a JSON number name and value pair.
      Throws:
      IOException
    • writeValue

      public JSONWriter writeValue(String name, boolean value) throws IOException
      Output a JSON boolean name and value pair.
      Throws:
      IOException
    • writeValue

      public JSONWriter writeValue(String value) throws IOException
      Output a JSON string value. NOTE: no name is written - call from within an array structure.
      Throws:
      IOException
    • writeValue

      public JSONWriter writeValue(int value) throws IOException
      Output a JSON number value. NOTE: no name is written - call from within an array structure.
      Throws:
      IOException
    • writeValue

      public JSONWriter writeValue(long value) throws IOException
      Output a JSON number value. NOTE: no name is written - call from within an array structure.
      Throws:
      IOException
    • writeValue

      public JSONWriter writeValue(float value) throws IOException
      Output a JSON number value. NOTE: no name is written - call from within an array structure.
      Throws:
      IOException
    • writeValue

      public JSONWriter writeValue(double value) throws IOException
      Output a JSON number value. NOTE: no name is written - call from within an array structure.
      Throws:
      IOException
    • writeValue

      public JSONWriter writeValue(boolean value) throws IOException
      Output a JSON boolean value. NOTE: no name is written - call from within an array structure.
      Throws:
      IOException
    • writeNullValue

      public JSONWriter writeNullValue() throws IOException
      Output a JSON null value. NOTE: no name is written - call from within an array structure.
      Throws:
      IOException
    • writeNullValue

      public JSONWriter writeNullValue(String name) throws IOException
      Output a JSON null value.
      Throws:
      IOException
    • writeRawValue

      public JSONWriter writeRawValue(RawValue value) throws IOException
      Output a JSON boolean value. NOTE: no name is written - call from within an array structure.
      Throws:
      IOException
    • encodeToJSON

      public static String encodeToJSON(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(Object obj, JSONWriter writer) throws IOException
      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

      public static String encodeJSONString(String s)
      Safely encode a JSON string value.
      Returns:
      encoded string, null is handled and returned as "".