grails.converters
Class JSON

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

public class JSON
extends AbstractConverter<JSONWriter>
implements Converter<JSONWriter>

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

Author:
Siegfried Puchbauer, Graeme Rocher

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

referenceStack

protected Stack<Object> referenceStack
Constructor Detail

JSON

public JSON()
Default Constructor for a JSON Converter


JSON

public JSON(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(Writer out)
            throws ConverterException
Directs the JSON Writer to the given Writer

Specified by:
render in interface Converter<JSONWriter>
Parameters:
out - the Writer
Throws:
ConverterException

render

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

Specified by:
render in interface Converter<JSONWriter>
Parameters:
response - a HttpServletResponse
Throws:
ConverterException

getWriter

public JSONWriter getWriter()
                     throws ConverterException
Specified by:
getWriter in interface Converter<JSONWriter>
Throws:
ConverterException

convertAnother

public void convertAnother(Object o)
                    throws ConverterException
Specified by:
convertAnother in interface Converter<JSONWriter>
Throws:
ConverterException

build

public void build(groovy.lang.Closure c)
           throws ConverterException
Specified by:
build in interface Converter<JSONWriter>
Throws:
ConverterException

value

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

getDepth

public int getDepth()

property

public void property(String key,
                     Object value)
              throws JSONException,
                     ConverterException
Throws:
JSONException
ConverterException

toString

public String toString(boolean prettyPrint)
                throws 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:
JSONException

parse

public static JSONElement parse(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 JSONElement parse(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 JSONElement parse(InputStream is,
                                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 Object parse(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(Object target)
Sets the Object which is later converted to JSON

Specified by:
setTarget in class AbstractConverter<JSONWriter>
Parameters:
target - the Object
See Also:
Converter

handleCircularRelationship

protected void handleCircularRelationship(Object o)
                                   throws ConverterException
Throws:
ConverterException

getNamedConfig

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

use

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

use

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

registerObjectMarshaller

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

registerObjectMarshaller

public static void registerObjectMarshaller(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(String name,
                                     groovy.lang.Closure callable)
                              throws ConverterException
Throws:
ConverterException

withDefaultConfiguration

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


Copyright (c) 2005-2009 The Grails project