Package: grails.converters

## [Java] Class JSON

- grails.converters.JSON

- All Implemented Interfaces and Traits:
    IncludeExcludeConverter

      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

    Nested classes
    Modifiers | Name | Description
    --- | --- | ---
    `class` | `JSON.Builder` |

  - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `protected CircularReferenceBehaviour` | `circularReferenceBehaviour` |
    `protected ConverterConfiguration<JSON>` | `config` |
    `protected boolean` | `prettyPrint` |
    `protected java.util.Stack<java.lang.Object>` | `referenceStack` |
    `protected java.lang.Object` | `target` |
    `protected JSONWriter` | `writer` |

  - Inherited fields
    Fields inherited from class | Fields
    --- | ---
    `class AbstractConverter` | `contentType, encoding, excludes, includes`

  - ### Constructor Summary

    Constructors
    Constructor and description
    `JSON ()` Default Constructor for a JSON Converter
    `JSON (java.lang.Object target)` Creates a new JSON Converter for the given Object

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `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)`
    `` | `JSONWriter` | `getWriter()`
    `` | `protected void` | `handleCircularRelationship(java.lang.Object o)`
    `` | `protected ConverterConfiguration<JSON>` | `initConfig()`
    `` | `ObjectMarshaller<JSON>` | `lookupObjectMarshaller(java.lang.Object target)`
    `` | `static JSONElement` | `parse(java.io.Reader reader)` Parses the given JSON String and returns either a JSONObject or a JSONArray
    `` | `static JSONElement` | `parse(java.lang.String source)` Parses the given JSON String and returns either a JSONObject or a JSONArray
    `` | `static JSONElement` | `parse(java.io.InputStream is, java.lang.String encoding)` Parses the given JSON and returns either a JSONObject or a JSONArray
    `` | `static java.lang.Object` | `parse(javax.servlet.http.HttpServletRequest request)` Parses the given request's InputStream and returns either a JSONObject or a JSONArray
    `` | `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(java.io.Writer out)` Directs the JSON Writer to the given Writer
    `` | `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` | `setExcludes(java.util.List<java.lang.String> excludes)`
    `` | `void` | `setIncludes(java.util.List<java.lang.String> includes)`
    `` | `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)`

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class AbstractConverter` | `createBeanWrapper, getExcludes, getIncludes, setContentType, setEncoding, setExcludes, setIncludes, setTarget, toString, writeTo`
    `class java.lang.Object` | `java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()`

- - ### Field Detail

    - #### protected final CircularReferenceBehaviour circularReferenceBehaviour

    - #### protected final ConverterConfiguration<JSON> config

    - #### protected boolean prettyPrint

    - #### protected java.util.Stack<java.lang.Object> referenceStack

    - #### protected java.lang.Object target

    - #### protected JSONWriter writer

  - ### Constructor Detail

    - #### public JSON()

      Default Constructor for a JSON Converter

    - #### public JSON(java.lang.Object target)

      Creates a new JSON Converter for the given Object

      Parameters:
        `target` - the Object to convert

  - ### Method Detail

    - #### public void build(@java.lang.SuppressWarnings("rawtypes") groovy.lang.Closure c)

    - #### public void convertAnother(java.lang.Object o)

    - #### public static void createNamedConfig(java.lang.String name, groovy.lang.Closure<?> callable)

    - #### public int getDepth()

    - #### public static ConverterConfiguration<JSON> getNamedConfig(java.lang.String configName)

    - #### public JSONWriter getWriter()

    - #### protected void handleCircularRelationship(java.lang.Object o)

    - #### protected ConverterConfiguration<JSON> initConfig()

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

    - #### public static JSONElement parse(java.io.Reader reader)

      Parses the given JSON String and returns either a JSONObject or a JSONArray

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

    - #### public static JSONElement parse(java.lang.String source)

      Parses the given JSON String and returns either a JSONObject or a JSONArray

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

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

      Parses the given JSON and returns either a JSONObject or a JSONArray

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

    - #### public static java.lang.Object parse(javax.servlet.http.HttpServletRequest request)

      Parses the given request's InputStream and returns either a JSONObject or a JSONArray

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

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

    - #### public static void registerObjectMarshaller(java.lang.Class<?> clazz, groovy.lang.Closure<?> callable)

    - #### public static void registerObjectMarshaller(java.lang.Class<?> clazz, int priority, groovy.lang.Closure<?> callable)

    - #### public static void registerObjectMarshaller(ObjectMarshaller<JSON> om)

    - #### public static void registerObjectMarshaller(ObjectMarshaller<JSON> om, int priority)

    - #### public void render(java.io.Writer out)

      Directs the JSON Writer to the given Writer

      throws:
        org.grails.web.converters.exceptions.ConverterException
      Parameters:
        `out` - the Writer

    - #### public 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

      throws:
        ConverterException
      Parameters:
        `response` - a HttpServletResponse

    - #### @java.lang.Override public void setExcludes(java.util.List<java.lang.String> excludes)

    - #### @java.lang.Override public void setIncludes(java.util.List<java.lang.String> includes)

    - #### public void setPrettyPrint(boolean prettyPrint)

    - #### @java.lang.Override public void setTarget(java.lang.Object target)

      Sets the Object which is later converted to JSON

      Parameters:
        `target` - the Object
      See Also:
        Converter

    - #### public java.lang.String toString(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

    - #### public static java.lang.Object use(java.lang.String configName, groovy.lang.Closure<?> callable)

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

    - #### public void value(java.lang.Object o)

      throws:
        ConverterException
      Parameters:
        o

    - #### public static void withDefaultConfiguration(groovy.lang.Closure<?> callable)