Package groovy.json

Class StreamingJsonBuilder.StreamingJsonDelegate

  • All Implemented Interfaces:
    groovy.lang.GroovyObject
    Enclosing class:
    StreamingJsonBuilder

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

      • writer

        protected final Writer writer
      • first

        protected boolean first
      • state

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

      • StreamingJsonDelegate

        public StreamingJsonDelegate​(Writer w,
                                     boolean first)
      • StreamingJsonDelegate

        public StreamingJsonDelegate​(Writer w,
                                     boolean first,
                                     JsonGenerator generator)
    • Method Detail

      • getWriter

        public Writer getWriter()
        Returns:
        Obtains the current writer
      • call

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

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

        public void call​(String name,
                         Iterable coll,
                         @DelegatesTo(StreamingJsonDelegate.class)
                         groovy.lang.Closure c)
                  throws 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:
        IOException
      • call

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

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

        protected void verifyValue()
      • isCollectionWithClosure

        public static boolean isCollectionWithClosure​(Object[] args)
      • cloneDelegateAndGetContent

        public static void cloneDelegateAndGetContent​(Writer w,
                                                      @DelegatesTo(StreamingJsonDelegate.class)
                                                      groovy.lang.Closure c,
                                                      boolean first)