Overview | Package | Class | Deprecated | Index | Help
| Groovy Documentation
| FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | PROPERTY | CONSTR | METHOD | DETAIL: FIELD | PROPERTY | CONSTR | METHOD

## groovy.json
[Groovy] Class StreamingJsonBuilder

    java.lang.Object
      groovy.json.StreamingJsonBuilder

    class StreamingJsonBuilder

A builder for creating JSON payloads.

This builder supports the usual builder syntax made of nested method calls and closures, but also some specific aspects of JSON data structures, such as list of values, etc. Please make sure to have a look at the various methods provided by this builder to be able to learn about the various possibilities of usage.

Unlike the JsonBuilder class which creates a data structure in memory, which is handy in those situations where you want to alter the structure programmatically before output, the StreamingJsonBuilder streams to a writter directly without any memory data structure. So if you don't need to modify the structure, and want a more memory-efficient approach, please use the StreamingJsonBuilder.

Example:

         new StringWriter().with { w ->
             def builder = new groovy.json.StreamingJsonBuilder( w )
             builder.people {
                 person {
                     firstName 'Tim'
                     lastName 'Yates'
                     // Named arguments are valid values for objects too
                     address(
                         city: 'Manchester',
                         country: 'UK',
                         zip: 'M1 2AB',
                     )
                     living true
                     eyes 'left', 'right'
                 }
             }

             assert w.toString() == '{"people":{"person":{"firstName":"Tim","lastName":"Yates","address":{"city":"Manchester","country":"UK","zip":"M1 2AB"},"living":true,"eyes":["left","right"]}}}'
        }

Authors:
  Tim Yates

Since:
  1.8.1

Property Summary
`java.io.Writer` | `writer`

Constructor Summary
`StreamingJsonBuilder(java.io.Writer writer, java.lang.Object content = null)` Instantiates a JSON builder, possibly with some existing data structure.

Method Summary
`java.lang.Object` | `call(java.util.Map m)` Named arguments can be passed to the JSON builder instance to create a root JSON object
`java.lang.Object` | `call(java.util.List l)` A list of elements as arguments to the JSON builder creates a root JSON array
`java.lang.Object` | `call(java.lang.Object... args)` Varargs elements as arguments to the JSON builder create a root JSON array
`java.lang.Object` | `call(Closure c)` A closure passed to a JSON builder will create a root JSON object
`java.lang.Object` | `invokeMethod(java.lang.String name, java.lang.Object args)` A method call on the JSON builder instance will create a root object with only one key whose name is the name of the method being called.

Methods inherited from class java.lang.Object
java.lang.Object#wait(long), java.lang.Object#wait(long, int), 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()

Property Detail

### writer

    java.io.Writer writer

Constructor Detail

### StreamingJsonBuilder

    StreamingJsonBuilder(java.io.Writer writer, java.lang.Object content = null)

  Instantiates a JSON builder, possibly with some existing data structure.
  Parameters:
    `writer` - A writer to which Json will be written
    `content` - a pre-existing data structure, default to null

Method Detail

### call

    java.lang.Object call(java.util.Map m)

  Named arguments can be passed to the JSON builder instance to create a root JSON object

  Example:

       new StringWriter().with { w ->
         def json = new groovy.json.StreamingJsonBuilder( w )
         json name: "Tim", age: 31

         assert w.toString() == '{"name":"Tim","age":31}'
       }

  Parameters:
    `m` - a map of key / value pairs
  Returns:
    a map of key / value pairs

### call

    java.lang.Object call(java.util.List l)

  A list of elements as arguments to the JSON builder creates a root JSON array

  Example:

       new StringWriter().with { w ->
         def json = new groovy.json.StreamingJsonBuilder( w )
         def result = json([1, 2, 3])

         assert result == [ 1, 2, 3 ]
         assert w.toString() == "[1,2,3]"
       }

  Parameters:
    `l` - a list of values
  Returns:
    a list of values

### call

    java.lang.Object call(java.lang.Object... args)

  Varargs elements as arguments to the JSON builder create a root JSON array

  Example:

       new StringWriter().with { w ->
         def json = new groovy.json.StreamingJsonBuilder( w )
         def result = json 1, 2, 3

         assert result instanceof List
         assert w.toString() == "[1,2,3]"
       }

  Parameters:
    `args` - an array of values
  Returns:
    a list of values

### call

    java.lang.Object call(Closure c)

  A closure passed to a JSON builder will create a root JSON object

  Example:

       new StringWriter().with { w ->
         def json = new groovy.json.StreamingJsonBuilder( w )
         json {
            name "Tim"
            age 39
         }

         assert w.toString() == '{"name":"Tim","age":39}'
       }

  Parameters:
    `c` - a closure whose method call statements represent key / values of a JSON object

### invokeMethod

    java.lang.Object invokeMethod(java.lang.String name, java.lang.Object args)

  A method call on the JSON builder instance will create a root object with only one key whose name is the name of the method being called. This method takes as arguments:
  - a closure
  - a map (ie. named arguments)
  - a map and a closure
  - or no argument at all

  Example with a classicala builder-style:

       new StringWriter().with { w ->
           def json = new groovy.json.StreamingJsonBuilder( w )
           json.person {
               name "Tim"
                age 28
           }

           assert w.toString() == '{"person":{"name":"Tim","age":28}}'
       }

  Or alternatively with a method call taking named arguments:

       new StringWriter().with { w ->
           def json = new groovy.json.StreamingJsonBuilder( w )
           json.person name: "Tim", age: 32

           assert w.toString() == '{"person":{"name":"Tim","age":32}}'
       }

  If you use named arguments and a closure as last argument, the key/value pairs of the map (as named arguments) and the key/value pairs represented in the closure will be merged together — the closure properties overriding the map key/values in case the same key is used.

       new StringWriter().with { w ->
           def json = new groovy.json.StreamingJsonBuilder( w )
           json.person(name: "Tim", age: 35) { town "Manchester" }

           assert w.toString() == '{"person":{"name":"Tim","age":35,"town":"Manchester"}}'
       }

  The empty args call will create a key whose value will be an empty JSON object:

       new StringWriter().with { w ->
           def json = new groovy.json.StreamingJsonBuilder( w )
           json.person()

           assert w.toString() == '{"person":{}}'
       }

  Parameters:
    `name` - the single key
    `args` - the value associated with the key

Groovy Documentation