org.eclipse.jetty.util.ajax
类 JSON

java.lang.Object
  继承者 org.eclipse.jetty.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.Convertible may be implemented by classes that wish to externalize and initialize specific fields to and from JSON objects. Only directed acyclic graphs of objects are supported.

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(Appendable) to generate the JSON. The class JSON.Literal may be used to hold pre-generated JSON object.

The interface JSON.Convertor may be implemented to provide static converters for objects that may be registered with registerConvertor(Class, Convertor). These converters are looked up by class, interface and super class by getConvertor(Class).

If a JSON object has a "class" field, then a java class for that name is loaded and the method convertTo(Class,Map) is used to find a JSON.Convertor for that class.

If a JSON object has a "x-class" field then a direct lookup for a JSON.Convertor for that class name is done (without loading the class).


嵌套类摘要
static interface JSON.Convertible
          JSON Convertible object.
static interface JSON.Convertor
          Static JSON Convertor.
static interface JSON.Generator
          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.
static interface JSON.Output
          JSON Output class for use by JSON.Convertible.
static class JSON.ReaderSource
           
static interface JSON.Source
           
static class JSON.StringSource
           
 
字段摘要
static JSON DEFAULT
           
 
构造方法摘要
JSON()
           
 
方法摘要
 void addConvertor(Class forClass, JSON.Convertor convertor)
          Register a JSON.Convertor for a class or interface.
 void addConvertorFor(String name, JSON.Convertor convertor)
          Register a JSON.Convertor for a named class or interface.
 void append(Appendable buffer, Object object)
          Append object as JSON to string buffer.
 void append(StringBuffer buffer, Object object)
          已过时。 
 void appendArray(Appendable buffer, Collection collection)
           
 void appendArray(Appendable buffer, Object array)
           
 void appendArray(StringBuffer buffer, Collection collection)
          已过时。 
 void appendArray(StringBuffer buffer, Object array)
          已过时。 
 void appendBoolean(Appendable buffer, Boolean b)
           
 void appendBoolean(StringBuffer buffer, Boolean b)
          已过时。 
 void appendJSON(Appendable buffer, JSON.Convertible converter)
           
 void appendJSON(Appendable buffer, JSON.Convertor convertor, Object object)
           
 void appendJSON(Appendable buffer, JSON.Generator generator)
           
 void appendJSON(StringBuffer buffer, JSON.Convertible converter)
          已过时。 
 void appendJSON(StringBuffer buffer, JSON.Convertor convertor, Object object)
          已过时。 
 void appendJSON(StringBuffer buffer, JSON.Generator generator)
          已过时。 
 void appendMap(Appendable buffer, Map<?,?> map)
           
 void appendMap(StringBuffer buffer, Map<?,?> map)
          已过时。 
 void appendNull(Appendable buffer)
           
 void appendNull(StringBuffer buffer)
          已过时。 
 void appendNumber(Appendable buffer, Number number)
           
 void appendNumber(StringBuffer buffer, Number number)
          已过时。 
 void appendString(Appendable buffer, String string)
           
 void appendString(StringBuffer buffer, String string)
          已过时。 
protected static void complete(String seek, JSON.Source source)
           
protected  JSON contextFor(String field)
           
protected  JSON contextForArray()
           
protected  Object convertTo(Class type, Map map)
           
 Object fromJSON(String json)
          Convert JSON to Object
protected  JSON.Convertor getConvertor(Class forClass)
          Lookup a convertor for a class.
 JSON.Convertor getConvertorFor(String name)
          Lookup a convertor for a named class.
static JSON getDefault()
           
 int getStringBufferSize()
           
protected  Object handleUnknown(JSON.Source source, char c)
           
protected  Object[] newArray(int size)
           
protected  Map<String,Object> newMap()
           
static Object parse(InputStream in)
          已过时。 use parse(Reader)
static Object parse(InputStream in, boolean stripOuterComment)
          已过时。 use parse(Reader, boolean)
 Object parse(JSON.Source source)
           
 Object parse(JSON.Source source, boolean stripOuterComment)
           
static Object parse(Reader in)
           
static Object parse(Reader in, boolean stripOuterComment)
           
static Object parse(String s)
           
static Object parse(String s, boolean stripOuterComment)
           
protected  Object parseArray(JSON.Source source)
           
 Number parseNumber(JSON.Source source)
           
protected  Object parseObject(JSON.Source source)
           
protected  String parseString(JSON.Source source)
           
static void registerConvertor(Class forClass, JSON.Convertor convertor)
          Register a JSON.Convertor for a class or interface.
protected  void seekTo(char seek, JSON.Source source)
           
protected  char seekTo(String seek, JSON.Source source)
           
static void setDefault(JSON json)
          已过时。 
 void setStringBufferSize(int stringBufferSize)
           
 String toJSON(Object object)
          Convert Object to JSON
protected  String toString(char[] buffer, int offset, int length)
           
static String toString(Map object)
           
static String toString(Object object)
           
static String toString(Object[] array)
           
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

DEFAULT

public static final JSON DEFAULT
构造方法详细信息

JSON

public JSON()
方法详细信息

getStringBufferSize

public int getStringBufferSize()
返回:
the initial stringBuffer size to use when creating JSON strings (default 1024)

setStringBufferSize

public void setStringBufferSize(int stringBufferSize)
参数:
stringBufferSize - the initial stringBuffer size to use when creating JSON strings (default 1024)

registerConvertor

public static void registerConvertor(Class forClass,
                                     JSON.Convertor convertor)
Register a JSON.Convertor for a class or interface.

参数:
forClass - The class or interface that the convertor applies to
convertor - the convertor

getDefault

public static JSON getDefault()

setDefault

@Deprecated
public static void setDefault(JSON json)
已过时。 


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)
参数:
s - String containing JSON object or array.
返回:
A Map, Object array or primitive array parsed from the JSON.

parse

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

parse

public static Object parse(Reader in)
                    throws IOException
参数:
in - Reader containing JSON object or array.
返回:
A Map, Object array or primitive array parsed from the JSON.
抛出:
IOException

parse

public static Object parse(Reader in,
                           boolean stripOuterComment)
                    throws IOException
参数:
in - Reader containing JSON object or array.
stripOuterComment - If true, an outer comment around the JSON is ignored.
返回:
A Map, Object array or primitive array parsed from the JSON.
抛出:
IOException

parse

@Deprecated
public static Object parse(InputStream in)
                    throws IOException
已过时。 use parse(Reader)

参数:
in - Reader containing JSON object or array.
返回:
A Map, Object array or primitive array parsed from the JSON.
抛出:
IOException

parse

@Deprecated
public static Object parse(InputStream in,
                                      boolean stripOuterComment)
                    throws IOException
已过时。 use parse(Reader, boolean)

参数:
in - Stream containing JSON object or array.
stripOuterComment - If true, an outer comment around the JSON is ignored.
返回:
A Map, Object array or primitive array parsed from the JSON.
抛出:
IOException

toJSON

public String toJSON(Object object)
Convert Object to JSON

参数:
object - The object to convert
返回:
The JSON String

fromJSON

public Object fromJSON(String json)
Convert JSON to Object

参数:
json - The json to convert
返回:
The object

append

@Deprecated
public void append(StringBuffer buffer,
                              Object object)
已过时。 


append

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

参数:
buffer - the buffer to append to
object - the object to append

appendNull

@Deprecated
public void appendNull(StringBuffer buffer)
已过时。 


appendNull

public void appendNull(Appendable buffer)

appendJSON

@Deprecated
public void appendJSON(StringBuffer buffer,
                                  JSON.Convertor convertor,
                                  Object object)
已过时。 


appendJSON

public void appendJSON(Appendable buffer,
                       JSON.Convertor convertor,
                       Object object)

appendJSON

@Deprecated
public void appendJSON(StringBuffer buffer,
                                  JSON.Convertible converter)
已过时。 


appendJSON

public void appendJSON(Appendable buffer,
                       JSON.Convertible converter)

appendJSON

@Deprecated
public void appendJSON(StringBuffer buffer,
                                  JSON.Generator generator)
已过时。 


appendJSON

public void appendJSON(Appendable buffer,
                       JSON.Generator generator)

appendMap

@Deprecated
public void appendMap(StringBuffer buffer,
                                 Map<?,?> map)
已过时。 


appendMap

public void appendMap(Appendable buffer,
                      Map<?,?> map)

appendArray

@Deprecated
public void appendArray(StringBuffer buffer,
                                   Collection collection)
已过时。 


appendArray

public void appendArray(Appendable buffer,
                        Collection collection)

appendArray

@Deprecated
public void appendArray(StringBuffer buffer,
                                   Object array)
已过时。 


appendArray

public void appendArray(Appendable buffer,
                        Object array)

appendBoolean

@Deprecated
public void appendBoolean(StringBuffer buffer,
                                     Boolean b)
已过时。 


appendBoolean

public void appendBoolean(Appendable buffer,
                          Boolean b)

appendNumber

@Deprecated
public void appendNumber(StringBuffer buffer,
                                    Number number)
已过时。 


appendNumber

public void appendNumber(Appendable buffer,
                         Number number)

appendString

@Deprecated
public void appendString(StringBuffer buffer,
                                    String string)
已过时。 


appendString

public void appendString(Appendable buffer,
                         String string)

toString

protected String toString(char[] buffer,
                          int offset,
                          int length)

newMap

protected Map<String,Object> newMap()

newArray

protected Object[] newArray(int size)

contextForArray

protected JSON contextForArray()

contextFor

protected JSON contextFor(String field)

convertTo

protected Object convertTo(Class type,
                           Map map)

addConvertor

public void addConvertor(Class forClass,
                         JSON.Convertor convertor)
Register a JSON.Convertor for a class or interface.

参数:
forClass - The class or interface that the convertor applies to
convertor - the convertor

getConvertor

protected JSON.Convertor getConvertor(Class forClass)
Lookup a convertor for a class.

If no match is found for the class, then the interfaces for the class are tried. If still no match is found, then the super class and it's interfaces are tried recursively.

参数:
forClass - The class
返回:
a JSON.Convertor or null if none were found.

addConvertorFor

public void addConvertorFor(String name,
                            JSON.Convertor convertor)
Register a JSON.Convertor for a named class or interface.

参数:
name - name of a class or an interface that the convertor applies to
convertor - the convertor

getConvertorFor

public JSON.Convertor getConvertorFor(String name)
Lookup a convertor for a named class.

参数:
name - name of the class
返回:
a JSON.Convertor or null if none were found.

parse

public Object parse(JSON.Source source,
                    boolean stripOuterComment)

parse

public Object parse(JSON.Source source)

handleUnknown

protected Object handleUnknown(JSON.Source source,
                               char c)

parseObject

protected Object parseObject(JSON.Source source)

parseArray

protected Object parseArray(JSON.Source source)

parseString

protected String parseString(JSON.Source source)

parseNumber

public Number parseNumber(JSON.Source source)

seekTo

protected void seekTo(char seek,
                      JSON.Source source)

seekTo

protected char seekTo(String seek,
                      JSON.Source source)

complete

protected static void complete(String seek,
                               JSON.Source source)


Copyright © 2013. All Rights Reserved.