org.mortbay.util.ajax
Class JSON

java.lang.Object
  extended by org.mortbay.util.ajax.JSON

public class JSON
extends Object

JSON Parser and Generator.

This class provides some static methods to convert POJOs to and from JSON notation. The mapping from JSON to java is:

   object ==> Map
   array  ==> Object[]
   number ==> Double or Long
   string ==> String
   null   ==> null
   bool   ==> Boolean
 

The java to JSON mapping is:

   String --> string
   Number --> number
   Map    --> object
   List   --> array
   Array  --> array
   null   --> null
   Boolean--> boolean
   Object --> string (dubious!)
 

The interface JSON.Generator may be implemented by classes that know how to render themselves as JSON and the toString(Object) method will use JSON.Generator.addJSON(StringBuffer) to generate the JSON. The class JSON.Literal may be used to hold pre-gnerated JSON object.

Author:
gregw

Nested Class Summary
static interface JSON.Generator
           
static class JSON.Literal
          A Literal JSON generator A utility instance of JSON.Generator that holds a pre-generated string on JSON text.
 
Method Summary
static void append(StringBuffer buffer, Object object)
          Append object as JSON to string buffer.
static Object parse(InputStream in)
           
static Object parse(InputStream in, boolean stripOuterComment)
           
static Object parse(String s)
           
static Object parse(String s, boolean stripOuterComment)
           
static String toString(Map object)
           
static String toString(Object object)
           
static String toString(Object[] array)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

toString

public static String toString(Object object)

toString

public static String toString(Map object)

toString

public static String toString(Object[] array)

parse

public static Object parse(String s,
                           boolean stripOuterComment)
Parameters:
s - String containing JSON object or array.
stripOuterComment - If true, an outer comment around the JSON is ignored.
Returns:
A Map, Object array or primitive array parsed from the JSON.

parse

public static Object parse(InputStream in,
                           boolean stripOuterComment)
                    throws IOException
Parameters:
s - Stream containing JSON object or array.
stripOuterComment - If true, an outer comment around the JSON is ignored.
Returns:
A Map, Object array or primitive array parsed from the JSON.
Throws:
IOException

parse

public static Object parse(String s)
Parameters:
s - String containing JSON object or array.
Returns:
A Map, Object array or primitive array parsed from the JSON.

parse

public static Object parse(InputStream in)
                    throws IOException
Parameters:
s - Stream containing JSON object or array.
Returns:
A Map, Object array or primitive array parsed from the JSON.
Throws:
IOException

append

public static void append(StringBuffer buffer,
                          Object object)
Append object as JSON to string buffer.

Parameters:
buffer -
object -