Package: groovy.json

## [Java] Class StreamingJsonBuilder.StreamingJsonDelegate

- groovy.json.StreamingJsonBuilder.StreamingJsonDelegate

-     public static class StreamingJsonBuilder.StreamingJsonDelegate
      extends GroovyObjectSupport

  The delegate used when invoking closures

- - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `protected boolean` | `first` | Indicates whether the next entry is the first one in the current context.
    `protected StreamingJsonBuilder.StreamingJsonDelegate.State` | `state` | Tracks whether the next token should be a JSON name or value.
    `protected Writer` | `writer` | Writer receiving the streamed JSON output.

  - ### Constructor Summary

    Constructors
    Constructor and description
    `StreamingJsonDelegate(Writer w, boolean first)` Creates a delegate backed by the default generator.
    `StreamingJsonDelegate(Writer w, boolean first, JsonGenerator generator)` Creates a delegate backed by the supplied generator.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public void` | `call(String name, List<Object> list)` Writes the name and a JSON array
    `` | `public void` | `call(String name, Object array)` Writes the name and a JSON array
    `` | `public void` | `call(String name, Iterable coll, Closure c)` A collection and closure passed to a JSON builder will create a root JSON array applying the closure to each object in the collection
    `` | `public void` | `call(String name, Collection coll, Closure c)` Delegates to call(String, Iterable, Closure)
    `` | `public void` | `call(String name, Object value)` Writes the name and value of a JSON attribute
    `` | `public void` | `call(String name, Object value, Closure callable)` Writes the name and value of a JSON attribute
    `` | `public void` | `call(String name, Closure value)` Writes the name and another JSON object
    `` | `public void` | `call(String name, JsonUnescaped json)` Writes an unescaped value.
    `` | `public void` | `call(String name, Writable json)` Writes the given Writable as the value of the given attribute name
    `` | `public static void` | `cloneDelegateAndGetContent(Writer w, Closure c)` Clones a closure, assigns this delegate model, and writes its JSON content.
    `` | `public static void` | `cloneDelegateAndGetContent(Writer w, Closure c, boolean first)` Clones a closure, assigns this delegate model, and writes its JSON content.
    `` | `public static void` | `curryDelegateAndGetContent(Writer w, Closure c, Object o)` Curries an object into a closure, assigns this delegate model, and writes its JSON content.
    `` | `public static void` | `curryDelegateAndGetContent(Writer w, Closure c, Object o, boolean first)` Curries an object into a closure, assigns this delegate model, and writes its JSON content.
    `` | `public Writer` | `getWriter()` Returns: Obtains the current writer
    `` | `public Object` | `invokeMethod(String name, Object args)` Handles builder-style method calls for nested JSON output.
    `` | `public static boolean` | `isCollectionWithClosure(Object[] args)` Indicates whether the supplied arguments represent a collection and closure pair.
    `` | `protected void` | `verifyValue()` Verifies that the current output state expects a JSON value next.
    `` | `protected void` | `writeArray(List<Object> list)` Writes a JSON array value.
    `` | `public static Object` | `writeCollectionWithClosure(Writer writer, Collection coll, Closure closure)` Writes a JSON array by applying a closure to each element in the collection.
    `` | `protected void` | `writeName(String name)` Writes a JSON name and the following colon separator.
    `` | `protected void` | `writeValue(Object value)` Writes a scalar JSON value.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class GroovyObjectSupport` | `getMetaClass, setMetaClass`

- - ### Field Detail

    - #### protected boolean first

      Indicates whether the next entry is the first one in the current context.

    - #### protected StreamingJsonBuilder.StreamingJsonDelegate.State state

      Tracks whether the next token should be a JSON name or value.

    - #### protected final Writer writer

      Writer receiving the streamed JSON output.

  - ### Constructor Detail

    - #### public StreamingJsonDelegate(Writer w, boolean first)

      Creates a delegate backed by the default generator.

      Parameters:
        `w` - the writer receiving JSON output
        `first` - whether the next entry is the first entry

    - #### public StreamingJsonDelegate(Writer w, boolean first, JsonGenerator generator)

      Creates a delegate backed by the supplied generator.

      Parameters:
        `w` - the writer receiving JSON output
        `first` - whether the next entry is the first entry
        `generator` - the generator used to serialize scalar values

  - ### Method Detail

    - #### public void call(String name, List<Object> list)

      Writes the name and a JSON array

      throws:
        IOException
      Parameters:
        `name` - The name of the JSON attribute
        `list` - The list representing the array

    - #### public void call(String name, Object array)

      Writes the name and a JSON array

      throws:
        IOException
      Parameters:
        `name` - The name of the JSON attribute
        `array` - The list representing the array

    - #### public void call(String name, Iterable coll, @DelegatesTo(value = StreamingJsonDelegate.class, strategy = Closure.DELEGATE_FIRST)
      Closure c)

      A collection and closure passed to a JSON builder will create a root JSON array applying the closure to each object in the collection

      Example:

           class Author {
                String name
           }
           def authorList = [new Author (name: "Guillaume"), new Author (name: "Jochen"), new Author (name: "Paul")]

           new StringWriter().with { w ->
               def json = new groovy.json.StreamingJsonBuilder(w)
               json.book {
                  authors authorList, { Author author ->
                   name author.name
                 }
               }

               assert w.toString() == '{"book":{"authors":[{"name":"Guillaume"},{"name":"Jochen"},{"name":"Paul"}]}}'
           }

      Parameters:
        `coll` - a collection
        `c` - a closure used to convert the objects of coll

    - #### public void call(String name, Collection coll, @DelegatesTo(value = StreamingJsonDelegate.class, strategy = Closure.DELEGATE_FIRST)
      Closure c)

      Delegates to call(String, Iterable, Closure)

    - #### public void call(String name, Object value)

      Writes the name and value of a JSON attribute

      throws:
        IOException
      Parameters:
        `name` - The attribute name
        `value` - The value

    - #### public void call(String name, Object value, @DelegatesTo(value = StreamingJsonDelegate.class, strategy = Closure.DELEGATE_FIRST)
      Closure callable)

      Writes the name and value of a JSON attribute

      throws:
        IOException
      Parameters:
        `name` - The attribute name
        `value` - The value

    - #### public void call(String name, @DelegatesTo(value = StreamingJsonDelegate.class, strategy = Closure.DELEGATE_FIRST)
      Closure value)

      Writes the name and another JSON object

      throws:
        IOException
      Parameters:
        `name` - The attribute name
        `value` - The value

    - #### public void call(String name, JsonUnescaped json)

      Writes an unescaped value. Note: can cause invalid JSON if passed JSON is invalid

      throws:
        IOException
      Parameters:
        `name` - The attribute name
        `json` - The value

    - #### public void call(String name, Writable json)

      Writes the given Writable as the value of the given attribute name

      throws:
        IOException
      Parameters:
        `name` - The attribute name
        `json` - The writable value

    - #### public static void cloneDelegateAndGetContent(Writer w, @DelegatesTo(value = StreamingJsonDelegate.class, strategy = Closure.DELEGATE_FIRST)
      Closure c)

      Clones a closure, assigns this delegate model, and writes its JSON content.

      Parameters:
        `w` - the destination writer
        `c` - the closure describing the JSON object

    - #### public static void cloneDelegateAndGetContent(Writer w, @DelegatesTo(value = StreamingJsonDelegate.class, strategy = Closure.DELEGATE_FIRST)
      Closure c, boolean first)

      Clones a closure, assigns this delegate model, and writes its JSON content.

      Parameters:
        `w` - the destination writer
        `c` - the closure describing the JSON object
        `first` - whether the next entry is the first entry

    - #### public static void curryDelegateAndGetContent(Writer w, @DelegatesTo(value = StreamingJsonDelegate.class, strategy = Closure.DELEGATE_FIRST)
      Closure c, Object o)

      Curries an object into a closure, assigns this delegate model, and writes its JSON content.

      Parameters:
        `w` - the destination writer
        `c` - the closure describing the JSON object
        `o` - the object to curry into the closure

    - #### public static void curryDelegateAndGetContent(Writer w, @DelegatesTo(value = StreamingJsonDelegate.class, strategy = Closure.DELEGATE_FIRST)
      Closure c, Object o, boolean first)

      Curries an object into a closure, assigns this delegate model, and writes its JSON content.

      Parameters:
        `w` - the destination writer
        `c` - the closure describing the JSON object
        `o` - the object to curry into the closure
        `first` - whether the next entry is the first entry

    - #### public Writer getWriter()

      Returns:
        Obtains the current writer

    - #### public Object invokeMethod(String name, Object args)

      Handles builder-style method calls for nested JSON output.

      Parameters:
        `name` - the JSON name being written
        `args` - the value, values, or closure associated with the name
      Returns:
        this delegate

    - #### public static boolean isCollectionWithClosure(Object[] args)

      Indicates whether the supplied arguments represent a collection and closure pair.

      Parameters:
        `args` - the arguments to inspect
      Returns:
        `true` if the arguments are a collection plus closure pair

    - #### protected void verifyValue()

      Verifies that the current output state expects a JSON value next.

    - #### protected void writeArray(List<Object> list)

      Writes a JSON array value.

      throws:
        IOException if writing fails
      Parameters:
        `list` - the list to serialize

    - #### public static Object writeCollectionWithClosure(Writer writer, Collection coll, @DelegatesTo(value = StreamingJsonDelegate.class, strategy = Closure.DELEGATE_FIRST)
      Closure closure)

      Writes a JSON array by applying a closure to each element in the collection.

      throws:
        IOException if writing fails
      Parameters:
        `writer` - the destination writer
        `coll` - the collection to serialize
        `closure` - the closure used to build each JSON object entry
      Returns:
        the supplied writer

    - #### protected void writeName(String name)

      Writes a JSON name and the following colon separator.

      throws:
        IOException if writing fails
      Parameters:
        `name` - the JSON name to write

    - #### protected void writeValue(Object value)

      Writes a scalar JSON value.

      throws:
        IOException if writing fails
      Parameters:
        `value` - the value to write