grails.converters
Class JSON

java.lang.Object
  extended by org.codehaus.groovy.grails.web.converters.AbstractConverter<org.codehaus.groovy.grails.web.json.JSONWriter>
      extended by grails.converters.JSON
All Implemented Interfaces:
Converter<org.codehaus.groovy.grails.web.json.JSONWriter>
Direct Known Subclasses:
JSON

public class JSON
extends AbstractConverter<org.codehaus.groovy.grails.web.json.JSONWriter>

A converter that converts domain classes, Maps, Lists, Arrays, POJOs and POGOs to JSON.


Nested Class Summary
 class JSON.Builder
           
 
Nested classes/interfaces inherited from interface org.codehaus.groovy.grails.web.converters.Converter
Converter.CircularReferenceBehaviour
 
Field Summary
protected  java.util.Stack<java.lang.Object> referenceStack
           
protected  org.codehaus.groovy.grails.web.json.JSONWriter writer
           
 
Fields inherited from interface org.codehaus.groovy.grails.web.converters.Converter
DEFAULT_REQUEST_ENCODING
 
Constructor Summary
JSON()
          Default Constructor for a JSON Converter
JSON(java.lang.Object target)
          Creates a new JSON Converter for the given Object
 
Method Summary
 void build(groovy.lang.Closure c)
           
 void convertAnother(java.lang.Object o)
           
static void createNamedConfig(java.lang.String name, groovy.lang.Closure<?> callable)
           
 int getDepth()
           
static ConverterConfiguration<JSON> getNamedConfig(java.lang.String configName)
           
 org.codehaus.groovy.grails.web.json.JSONWriter getWriter()
           
protected  void handleCircularRelationship(java.lang.Object o)
           
protected  ConverterConfiguration<JSON> initConfig()
           
 ObjectMarshaller<JSON> lookupObjectMarshaller(java.lang.Object target)
           
static java.lang.Object parse(javax.servlet.http.HttpServletRequest request)
          Parses the given request's InputStream and returns ether a JSONObject or a JSONArry
static org.codehaus.groovy.grails.web.json.JSONElement parse(java.io.InputStream is, java.lang.String encoding)
          Parses the given JSON and returns ether a JSONObject or a JSONArry
static org.codehaus.groovy.grails.web.json.JSONElement parse(java.io.Reader reader)
          Parses the given JSON String and returns ether a JSONObject or a JSONArry
static org.codehaus.groovy.grails.web.json.JSONElement parse(java.lang.String source)
          Parses the given JSON String and returns ether a JSONObject or a JSONArry
 void property(java.lang.String key, java.lang.Object value)
           
static void registerObjectMarshaller(java.lang.Class<?> clazz, groovy.lang.Closure<?> callable)
           
static void registerObjectMarshaller(java.lang.Class<?> clazz, int priority, groovy.lang.Closure<?> callable)
           
static void registerObjectMarshaller(ObjectMarshaller<JSON> om)
           
static void registerObjectMarshaller(ObjectMarshaller<JSON> om, int priority)
           
 void render(javax.servlet.http.HttpServletResponse response)
          Directs the JSON Writer to the Outputstream of the HttpServletResponse and sets the Content-Type to application/json
 void render(java.io.Writer out)
          Directs the JSON Writer to the given Writer
 void setPrettyPrint(boolean prettyPrint)
           
 void setTarget(java.lang.Object target)
          Sets the Object which is later converted to JSON
 java.lang.String toString(boolean prettyPrint)
          Performs the conversion and returns the resulting JSON as String
static void use(java.lang.String cfgName)
           
static java.lang.Object use(java.lang.String configName, groovy.lang.Closure<?> callable)
           
 void value(java.lang.Object o)
           
static void withDefaultConfiguration(groovy.lang.Closure<?> callable)
           
 
Methods inherited from class org.codehaus.groovy.grails.web.converters.AbstractConverter
createBeanWrapper, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

writer

protected org.codehaus.groovy.grails.web.json.JSONWriter writer

referenceStack

protected java.util.Stack<java.lang.Object> referenceStack
Constructor Detail

JSON

public JSON()
Default Constructor for a JSON Converter


JSON

public JSON(java.lang.Object target)
Creates a new JSON Converter for the given Object

Parameters:
target - the Object to convert
Method Detail

initConfig

protected ConverterConfiguration<JSON> initConfig()

setPrettyPrint

public void setPrettyPrint(boolean prettyPrint)

render

public void render(java.io.Writer out)
            throws ConverterException
Directs the JSON Writer to the given Writer

Parameters:
out - the Writer
Throws:
ConverterException

render

public void render(javax.servlet.http.HttpServletResponse response)
            throws ConverterException
Directs the JSON Writer to the Outputstream of the HttpServletResponse and sets the Content-Type to application/json

Parameters:
response - a HttpServletResponse
Throws:
ConverterException

getWriter

public org.codehaus.groovy.grails.web.json.JSONWriter getWriter()
                                                         throws ConverterException
Throws:
ConverterException

convertAnother

public void convertAnother(java.lang.Object o)
                    throws ConverterException
Throws:
ConverterException

build

public void build(groovy.lang.Closure c)
           throws ConverterException
Throws:
ConverterException

value

public void value(java.lang.Object o)
           throws ConverterException
Parameters:
o -
Throws:
ConverterException

lookupObjectMarshaller

public ObjectMarshaller<JSON> lookupObjectMarshaller(java.lang.Object target)

getDepth

public int getDepth()

property

public void property(java.lang.String key,
                     java.lang.Object value)
              throws org.codehaus.groovy.grails.web.json.JSONException,
                     ConverterException
Throws:
org.codehaus.groovy.grails.web.json.JSONException
ConverterException

toString

public java.lang.String toString(boolean prettyPrint)
                          throws org.codehaus.groovy.grails.web.json.JSONException
Performs the conversion and returns the resulting JSON as String

Parameters:
prettyPrint - true, if the output should be indented, otherwise false
Returns:
a JSON String
Throws:
org.codehaus.groovy.grails.web.json.JSONException

parse

public static org.codehaus.groovy.grails.web.json.JSONElement parse(java.io.Reader reader)
                                                             throws ConverterException
Parses the given JSON String and returns ether a JSONObject or a JSONArry

Parameters:
reader - JSON source
Returns:
ether a JSONObject or a JSONArray - depending on the given JSON
Throws:
ConverterException - when the JSON content is not valid

parse

public static org.codehaus.groovy.grails.web.json.JSONElement parse(java.lang.String source)
                                                             throws ConverterException
Parses the given JSON String and returns ether a JSONObject or a JSONArry

Parameters:
source - A string containing some JSON
Returns:
ether a JSONObject or a JSONArray - depending on the given JSON
Throws:
ConverterException - when the JSON content is not valid

parse

public static org.codehaus.groovy.grails.web.json.JSONElement parse(java.io.InputStream is,
                                                                    java.lang.String encoding)
                                                             throws ConverterException
Parses the given JSON and returns ether a JSONObject or a JSONArry

Parameters:
is - An InputStream which delivers some JSON
encoding - the Character Encoding to use
Returns:
ether a JSONObject or a JSONArray - depending on the given JSON
Throws:
ConverterException - when the JSON content is not valid

parse

public static java.lang.Object parse(javax.servlet.http.HttpServletRequest request)
                              throws ConverterException
Parses the given request's InputStream and returns ether a JSONObject or a JSONArry

Parameters:
request - the JSON Request
Returns:
ether a JSONObject or a JSONArray - depending on the given JSON
Throws:
ConverterException - when the JSON content is not valid

setTarget

public void setTarget(java.lang.Object target)
Sets the Object which is later converted to JSON

Specified by:
setTarget in class AbstractConverter<org.codehaus.groovy.grails.web.json.JSONWriter>
Parameters:
target - the Object
See Also:
Converter

handleCircularRelationship

protected void handleCircularRelationship(java.lang.Object o)
                                   throws ConverterException
Throws:
ConverterException

getNamedConfig

public static ConverterConfiguration<JSON> getNamedConfig(java.lang.String configName)
                                                   throws ConverterException
Throws:
ConverterException

use

public static java.lang.Object use(java.lang.String configName,
                                   groovy.lang.Closure<?> callable)
                            throws ConverterException
Throws:
ConverterException

use

public static void use(java.lang.String cfgName)
                throws ConverterException
Throws:
ConverterException

registerObjectMarshaller

public static void registerObjectMarshaller(java.lang.Class<?> clazz,
                                            groovy.lang.Closure<?> callable)
                                     throws ConverterException
Throws:
ConverterException

registerObjectMarshaller

public static void registerObjectMarshaller(java.lang.Class<?> clazz,
                                            int priority,
                                            groovy.lang.Closure<?> callable)
                                     throws ConverterException
Throws:
ConverterException

registerObjectMarshaller

public static void registerObjectMarshaller(ObjectMarshaller<JSON> om)
                                     throws ConverterException
Throws:
ConverterException

registerObjectMarshaller

public static void registerObjectMarshaller(ObjectMarshaller<JSON> om,
                                            int priority)
                                     throws ConverterException
Throws:
ConverterException

createNamedConfig

public static void createNamedConfig(java.lang.String name,
                                     groovy.lang.Closure<?> callable)
                              throws ConverterException
Throws:
ConverterException

withDefaultConfiguration

public static void withDefaultConfiguration(groovy.lang.Closure<?> callable)
                                     throws ConverterException
Throws:
ConverterException