Groovy Documentation

grails.converters
[Java] Class JSON

java.lang.Object
  org.codehaus.groovy.grails.web.converters.AbstractConverter
      grails.converters.JSON
All Implemented Interfaces:
Converter

public class JSON
extends AbstractConverter

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

Authors:
Siegfried Puchbauer
Graeme Rocher


Nested Class Summary
class JSON.Builder

 
Field Summary
protected java.util.Stack referenceStack

protected JSONWriter writer

 
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 getNamedConfig(java.lang.String configName)

JSONWriter getWriter()

protected void handleCircularRelationship(java.lang.Object o)

protected ConverterConfiguration initConfig()

ObjectMarshaller lookupObjectMarshaller(java.lang.Object target)

static JSONElement parse(java.io.Reader reader)

Parses the given JSON String and returns ether a JSONObject or a JSONArry

static JSONElement parse(java.lang.String source)

Parses the given JSON String and returns ether a JSONObject or a JSONArry

static JSONElement parse(java.io.InputStream is, java.lang.String encoding)

Parses the given JSON and returns ether a JSONObject or a JSONArry

static java.lang.Object parse(HttpServletRequest request)

Parses the given request's InputStream 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 om)

static void registerObjectMarshaller(ObjectMarshaller om, int priority)

void render(java.io.Writer out)

Directs the JSON Writer to the given Writer

void render(HttpServletResponse response)

Directs the JSON Writer to the Outputstream of the HttpServletResponse and sets the Content-Type to application/json

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 java.lang.Object use(java.lang.String configName, groovy.lang.Closure callable)

static void use(java.lang.String cfgName)

void value(java.lang.Object o)

@param o

static void withDefaultConfiguration(groovy.lang.Closure callable)

 
Methods inherited from class AbstractConverter
createBeanWrapper, setTarget, toString
 

Field Detail

referenceStack

protected java.util.Stack referenceStack


writer

protected JSONWriter writer


 
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

build

public void build(groovy.lang.Closure c)


convertAnother

public void convertAnother(java.lang.Object o)


createNamedConfig

public static void createNamedConfig(java.lang.String name, groovy.lang.Closure callable)


getDepth

public int getDepth()


getNamedConfig

public static ConverterConfiguration getNamedConfig(java.lang.String configName)


getWriter

public JSONWriter getWriter()


handleCircularRelationship

protected void handleCircularRelationship(java.lang.Object o)


initConfig

protected ConverterConfiguration initConfig()


lookupObjectMarshaller

public ObjectMarshaller lookupObjectMarshaller(@SuppressWarnings("hiding") java.lang.Object target)


parse

public static JSONElement parse(java.io.Reader reader)
Parses the given JSON String and returns ether a JSONObject or a JSONArry
throws:
ConverterException when the JSON content is not valid
Parameters:
reader - JSON source
Returns:
ether a JSONObject or a JSONArray - depending on the given JSON


parse

public static JSONElement parse(java.lang.String source)
Parses the given JSON String and returns ether a JSONObject or a JSONArry
throws:
ConverterException when the JSON content is not valid
Parameters:
source - A string containing some JSON
Returns:
ether a JSONObject or a JSONArray - depending on the given JSON


parse

public static JSONElement parse(java.io.InputStream is, java.lang.String encoding)
Parses the given JSON and returns ether a JSONObject or a JSONArry
throws:
ConverterException when the JSON content is not valid
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


parse

public static java.lang.Object parse(HttpServletRequest request)
Parses the given request's InputStream and returns ether a JSONObject or a JSONArry
throws:
ConverterException when the JSON content is not valid
Parameters:
request - the JSON Request
Returns:
ether a JSONObject or a JSONArray - depending on the given JSON


property

public void property(java.lang.String key, java.lang.Object value)


registerObjectMarshaller

public static void registerObjectMarshaller(java.lang.Class clazz, groovy.lang.Closure callable)


registerObjectMarshaller

public static void registerObjectMarshaller(java.lang.Class clazz, int priority, groovy.lang.Closure callable)


registerObjectMarshaller

public static void registerObjectMarshaller(ObjectMarshaller om)


registerObjectMarshaller

public static void registerObjectMarshaller(ObjectMarshaller om, int priority)


render

public void render(java.io.Writer out)
Directs the JSON Writer to the given Writer
throws:
org.codehaus.groovy.grails.web.converters.exceptions.ConverterException
Parameters:
out - the Writer


render

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


setPrettyPrint

public void setPrettyPrint(boolean prettyPrint)


setTarget

@Override
public void setTarget(java.lang.Object target)
Sets the Object which is later converted to JSON
Parameters:
target - the Object
See Also:
Converter


toString

public java.lang.String toString(@SuppressWarnings("hiding") boolean prettyPrint)
Performs the conversion and returns the resulting JSON as String
throws:
JSONException
Parameters:
prettyPrint - true, if the output should be indented, otherwise false
Returns:
a JSON String


use

public static java.lang.Object use(java.lang.String configName, groovy.lang.Closure callable)


use

public static void use(java.lang.String cfgName)


value

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


withDefaultConfiguration

public static void withDefaultConfiguration(groovy.lang.Closure callable)


 

Groovy Documentation