Package groovy.json

Class StreamingJsonBuilder.StreamingJsonDelegate

    • Field Detail

      • writer

        protected final java.io.Writer writer
      • first

        protected boolean first
      • state

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

      • StreamingJsonDelegate

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

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

      • 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.
        Specified by:
        invokeMethod in interface GroovyObject
        Overrides:
        invokeMethod in class GroovyObjectSupport
        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(StreamingJsonDelegate.class)
                         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.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(StreamingJsonDelegate.class)
                         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(StreamingJsonDelegate.class)
                         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(StreamingJsonDelegate.class)
                                                                  Closure closure)
                                                           throws java.io.IOException
        Throws:
        java.io.IOException