grails.converters
Class JSON

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

public class JSON
extends AbstractConverter
implements Converter

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

Author:
Siegfried Puchbauer

Field Summary
 
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 array(Object o)
           
protected  void bean(Object o)
          Renders any Java object to the JSON Writer by leveraging the Java reflection API
protected  void domain(Object o)
          Renders a instance of a Grails Domain Class to the JSON Writer
protected  Object extractIdValue(Object domainObject, GrailsDomainClassProperty idProperty)
          Reads the value of the primary identifier property of a domain class instance
 boolean isRenderDomainClassRelations()
          Returns true if the JSON Converter is configured to convert referenced Domain Class instances as they are or just their id's (false)
static Object parse(HttpServletRequest request)
          Parses the given request's InputStream and returns ether a JSONObject or a JSONArry
static Object parse(InputStream is, String encoding)
          Parses the given JSON and returns ether a JSONObject or a JSONArry
static Object parse(String source)
          Parses the given JSON String and returns ether a JSONObject or a JSONArry
 void render(HttpServletResponse response)
          Directs the JSON Writer to the Outputstream of the HttpServletResponse and sets the Content-Type to text/json
 void render(Writer out)
          Directs the JSON Writer to the given Writer
 JSON setRenderDomainClassRelations(boolean renderDomainClassRelations)
           
 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
 void value(Object o)
           
 
Methods inherited from class org.codehaus.groovy.grails.web.converters.AbstractConverter
createBeanWrapper, isJdk5Enum, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

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

isRenderDomainClassRelations

public boolean isRenderDomainClassRelations()
Returns true if the JSON Converter is configured to convert referenced Domain Class instances as they are or just their id's (false)

Returns:
true or false ;-)

setRenderDomainClassRelations

public JSON setRenderDomainClassRelations(boolean renderDomainClassRelations)
Parameters:
renderDomainClassRelations -
Returns:
the JSON instance for chaining

render

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

Specified by:
render in interface Converter
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 text/json

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

extractIdValue

protected Object extractIdValue(Object domainObject,
                                GrailsDomainClassProperty idProperty)
Reads the value of the primary identifier property of a domain class instance

Parameters:
domainObject - The domain class instance
idProperty - The GrailsDomainClassProperty
Returns:
The primary identifier of the specified domain object

bean

protected void bean(Object o)
             throws ConverterException
Renders any Java object to the JSON Writer by leveraging the Java reflection API

Parameters:
o - The Bean to render
Throws:
ConverterException

domain

protected void domain(Object o)
               throws ConverterException
Renders a instance of a Grails Domain Class to the JSON Writer

Parameters:
o - The Domain Class instance
Throws:
ConverterException

array

public void array(Object o)
           throws ConverterException
Parameters:
o - the array object
Throws:
ConverterException

value

public void value(Object o)
           throws ConverterException
Parameters:
o -
Throws:
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 Object 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 Object 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
Parameters:
target - the Object
See Also:
Converter


Copyright (c) 2005-2006 The Grails project