Package groovy.json

Class StreamingJsonBuilder.StreamingJsonDelegate

java.lang.Object
groovy.lang.GroovyObjectSupport
groovy.json.StreamingJsonBuilder.StreamingJsonDelegate
All Implemented Interfaces:
GroovyObject
Enclosing class:
StreamingJsonBuilder

public static class StreamingJsonBuilder.StreamingJsonDelegate
extends GroovyObjectSupport
The delegate used when invoking closures
  • Field Summary

    Fields
    Modifier and Type Field Description
    protected boolean first  
    protected groovy.json.StreamingJsonBuilder.StreamingJsonDelegate.State state  
    protected java.io.Writer writer  
  • Constructor Summary

    Constructors
    Constructor Description
    StreamingJsonDelegate​(java.io.Writer w, boolean first)  
    StreamingJsonDelegate​(java.io.Writer w, boolean first, JsonGenerator generator)  
  • Method Summary

    Modifier and Type Method Description
    void call​(java.lang.String name, JsonOutput.JsonUnescaped json)
    Writes an unescaped value.
    void call​(java.lang.String name, Closure value)
    Writes the name and another JSON object
    void call​(java.lang.String name, Writable json)
    Writes the given Writable as the value of the given attribute name
    void call​(java.lang.String name, java.lang.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
    void call​(java.lang.String name, java.lang.Object value)
    Writes the name and value of a JSON attribute
    void call​(java.lang.String name, java.lang.Object... array)
    Writes the name and a JSON array
    void call​(java.lang.String name, java.lang.Object value, Closure callable)
    Writes the name and value of a JSON attribute
    void call​(java.lang.String name, java.util.Collection coll, Closure c)
    void call​(java.lang.String name, java.util.List<java.lang.Object> list)
    Writes the name and a JSON array
    static void cloneDelegateAndGetContent​(java.io.Writer w, Closure c)  
    static void cloneDelegateAndGetContent​(java.io.Writer w, Closure c, boolean first)  
    static void curryDelegateAndGetContent​(java.io.Writer w, Closure c, java.lang.Object o)  
    static void curryDelegateAndGetContent​(java.io.Writer w, Closure c, java.lang.Object o, boolean first)  
    java.io.Writer getWriter()  
    java.lang.Object invokeMethod​(java.lang.String name, java.lang.Object args)
    Invokes the given method.
    static boolean isCollectionWithClosure​(java.lang.Object[] args)  
    protected void verifyValue()  
    protected void writeArray​(java.util.List<java.lang.Object> list)  
    static java.lang.Object writeCollectionWithClosure​(java.io.Writer writer, java.util.Collection coll, Closure closure)  
    protected void writeName​(java.lang.String name)  
    protected void writeValue​(java.lang.Object value)  

    Methods inherited from class groovy.lang.GroovyObjectSupport

    getMetaClass, setMetaClass

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface groovy.lang.GroovyObject

    getProperty, setProperty
  • Field Details

    • writer

      protected final java.io.Writer writer
    • first

      protected boolean first
    • state

      protected groovy.json.StreamingJsonBuilder.StreamingJsonDelegate.State state
  • Constructor Details

    • StreamingJsonDelegate

      public StreamingJsonDelegate​(java.io.Writer w, boolean first)
    • StreamingJsonDelegate

      public StreamingJsonDelegate​(java.io.Writer w, boolean first, JsonGenerator generator)
  • Method Details

    • getWriter

      public java.io.Writer getWriter()
      Returns:
      Obtains the current writer
    • invokeMethod

      public java.lang.Object invokeMethod​(java.lang.String name, java.lang.Object args)
      Description copied from interface: GroovyObject
      Invokes the given method.
      Parameters:
      name - the name of the method to call
      args - the arguments to use for the method call
      Returns:
      the result of invoking the method
    • call

      public void call​(java.lang.String name, java.util.List<java.lang.Object> list) throws java.io.IOException
      Writes the name and a JSON array
      Parameters:
      name - The name of the JSON attribute
      list - The list representing the array
      Throws:
      java.io.IOException
    • call

      public void call​(java.lang.String name, java.lang.Object... array) throws java.io.IOException
      Writes the name and a JSON array
      Parameters:
      name - The name of the JSON attribute
      array - The list representing the array
      Throws:
      java.io.IOException
    • call

      public void call​(java.lang.String name, java.lang.Iterable coll, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c) throws java.io.IOException
      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
      Throws:
      java.io.IOException
    • call

      public void call​(java.lang.String name, java.util.Collection coll, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c) throws java.io.IOException
      Throws:
      java.io.IOException
    • call

      public void call​(java.lang.String name, java.lang.Object value) throws java.io.IOException
      Writes the name and value of a JSON attribute
      Parameters:
      name - The attribute name
      value - The value
      Throws:
      java.io.IOException
    • call

      public void call​(java.lang.String name, java.lang.Object value, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure callable) throws java.io.IOException
      Writes the name and value of a JSON attribute
      Parameters:
      name - The attribute name
      value - The value
      Throws:
      java.io.IOException
    • call

      public void call​(java.lang.String name, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure value) throws java.io.IOException
      Writes the name and another JSON object
      Parameters:
      name - The attribute name
      value - The value
      Throws:
      java.io.IOException
    • call

      public void call​(java.lang.String name, JsonOutput.JsonUnescaped json) throws java.io.IOException
      Writes an unescaped value. Note: can cause invalid JSON if passed JSON is invalid
      Parameters:
      name - The attribute name
      json - The value
      Throws:
      java.io.IOException
    • call

      public void call​(java.lang.String name, Writable json) throws java.io.IOException
      Writes the given Writable as the value of the given attribute name
      Parameters:
      name - The attribute name
      json - The writable value
      Throws:
      java.io.IOException
    • verifyValue

      protected void verifyValue()
    • writeName

      protected void writeName​(java.lang.String name) throws java.io.IOException
      Throws:
      java.io.IOException
    • writeValue

      protected void writeValue​(java.lang.Object value) throws java.io.IOException
      Throws:
      java.io.IOException
    • writeArray

      protected void writeArray​(java.util.List<java.lang.Object> list) throws java.io.IOException
      Throws:
      java.io.IOException
    • isCollectionWithClosure

      public static boolean isCollectionWithClosure​(java.lang.Object[] args)
    • writeCollectionWithClosure

      public static java.lang.Object writeCollectionWithClosure​(java.io.Writer writer, java.util.Collection coll, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure closure) throws java.io.IOException
      Throws:
      java.io.IOException
    • cloneDelegateAndGetContent

      public static void cloneDelegateAndGetContent​(java.io.Writer w, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c)
    • cloneDelegateAndGetContent

      public static void cloneDelegateAndGetContent​(java.io.Writer w, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c, boolean first)
    • curryDelegateAndGetContent

      public static void curryDelegateAndGetContent​(java.io.Writer w, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c, java.lang.Object o)
    • curryDelegateAndGetContent

      public static void curryDelegateAndGetContent​(java.io.Writer w, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c, java.lang.Object o, boolean first)