Class StreamingJsonBuilder
- All Implemented Interfaces:
- GroovyObject
public class StreamingJsonBuilder extends GroovyObjectSupport
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 programatically before output, the StreamingJsonBuilder streams to a writer 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"]}}}'
    }
 - Since:
- 1.8.1
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classStreamingJsonBuilder.StreamingJsonDelegateThe delegate used when invoking closures
- 
Constructor SummaryConstructors Constructor Description StreamingJsonBuilder(java.io.Writer writer)Instantiates a JSON builder.StreamingJsonBuilder(java.io.Writer writer, JsonGenerator generator)Instantiates a JSON builder with the given generator.StreamingJsonBuilder(java.io.Writer writer, java.lang.Object content)Instantiates a JSON builder, possibly with some existing data structure.StreamingJsonBuilder(java.io.Writer writer, java.lang.Object content, JsonGenerator generator)Instantiates a JSON builder, possibly with some existing data structure and the given generator.
- 
Method SummaryModifier and Type Method Description java.lang.Objectcall(Closure c)A closure passed to a JSON builder will create a root JSON objectjava.lang.Objectcall(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 collectionjava.lang.Objectcall(java.lang.Object... args)Varargs elements as arguments to the JSON builder create a root JSON arrayvoidcall(java.lang.String name)The empty args call will create a key whose value will be an empty JSON object:voidcall(java.lang.String name, Closure c)A name and a closure passed to a JSON builder will create a key with a JSON objectvoidcall(java.lang.String name, java.lang.Iterable coll, Closure c)A name, a collection and closure passed to a JSON builder will create a root JSON array applying the closure to each object in the collectionvoidcall(java.lang.String name, java.util.Collection coll, Closure c)Delegates tocall(String, Iterable, Closure)voidcall(java.lang.String name, java.util.Map map, Closure callable)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.java.lang.Objectcall(java.util.Collection coll, Closure c)Delegates tocall(Iterable, Closure)java.lang.Objectcall(java.util.List l)A list of elements as arguments to the JSON builder creates a root JSON arrayjava.lang.Objectcall(java.util.Map m)Named arguments can be passed to the JSON builder instance to create a root JSON objectjava.lang.ObjectinvokeMethod(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 groovy.lang.GroovyObjectSupportgetMetaClass, setMetaClassMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface groovy.lang.GroovyObjectgetProperty, setProperty
- 
Constructor Details- 
StreamingJsonBuilderpublic StreamingJsonBuilder(java.io.Writer writer)Instantiates a JSON builder.- Parameters:
- writer- A writer to which Json will be written
 
- 
StreamingJsonBuilderInstantiates a JSON builder with the given generator.- Parameters:
- writer- A writer to which Json will be written
- generator- used to generate the output
- Since:
- 2.5.0
 
- 
StreamingJsonBuilderpublic StreamingJsonBuilder(java.io.Writer writer, java.lang.Object content) throws java.io.IOExceptionInstantiates 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
- Throws:
- java.io.IOException
 
- 
StreamingJsonBuilderpublic StreamingJsonBuilder(java.io.Writer writer, java.lang.Object content, JsonGenerator generator) throws java.io.IOExceptionInstantiates a JSON builder, possibly with some existing data structure and the given generator.- Parameters:
- writer- A writer to which Json will be written
- content- a pre-existing data structure, default to null
- generator- used to generate the output
- Throws:
- java.io.IOException
- Since:
- 2.5.0
 
 
- 
- 
Method Details- 
callpublic java.lang.Object call(java.util.Map m) throws java.io.IOExceptionNamed arguments can be passed to the JSON builder instance to create a root JSON objectExample: 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
- Throws:
- java.io.IOException
 
- 
callpublic void call(java.lang.String name) throws java.io.IOExceptionThe 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 name of the empty object to create
- Throws:
- java.io.IOException
 
- 
callpublic java.lang.Object call(java.util.List l) throws java.io.IOExceptionA list of elements as arguments to the JSON builder creates a root JSON arrayExample: 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
- Throws:
- java.io.IOException
 
- 
callpublic java.lang.Object call(java.lang.Object... args) throws java.io.IOExceptionVarargs elements as arguments to the JSON builder create a root JSON arrayExample: 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
- Throws:
- java.io.IOException
 
- 
callpublic java.lang.Object call(java.lang.Iterable coll, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c) throws java.io.IOExceptionA collection and closure passed to a JSON builder will create a root JSON array applying the closure to each object in the collectionExample: class Author { String name } def authors = [new Author (name: "Guillaume"), new Author (name: "Jochen"), new Author (name: "Paul")] new StringWriter().with { w->def json = new groovy.json.StreamingJsonBuilder(w) json authors, { Author author->name author.name } assert w.toString() == '[{"name":"Guillaume"},{"name":"Jochen"},{"name":"Paul"}]' }- Parameters:
- coll- a collection
- c- a closure used to convert the objects of coll
- Throws:
- java.io.IOException
 
- 
callpublic java.lang.Object call(java.util.Collection coll, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c) throws java.io.IOExceptionDelegates tocall(Iterable, Closure)- Throws:
- java.io.IOException
 
- 
callpublic java.lang.Object call(@DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c) throws java.io.IOExceptionA closure passed to a JSON builder will create a root JSON objectExample: 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
- Throws:
- java.io.IOException
 
- 
callpublic void call(java.lang.String name, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c) throws java.io.IOExceptionA name and a closure passed to a JSON builder will create a key with a JSON objectExample: new StringWriter().with { w->def json = new groovy.json.StreamingJsonBuilder(w) json.person { name "Tim" age 39 } assert w.toString() == '{"person":{"name":"Tim","age":39}}' }- Parameters:
- name- The key for the JSON object
- c- a closure whose method call statements represent key / values of a JSON object
- Throws:
- java.io.IOException
 
- 
callpublic void call(java.lang.String name, java.lang.Iterable coll, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c) throws java.io.IOExceptionA name, a collection and closure passed to a JSON builder will create a root JSON array applying the closure to each object in the collectionExample: class Author { String name } def authors = [new Author (name: "Guillaume"), new Author (name: "Jochen"), new Author (name: "Paul")] new StringWriter().with { w->def json = new groovy.json.StreamingJsonBuilder(w) json.people authors, { Author author->name author.name } assert w.toString() == '{"people":[{"name":"Guillaume"},{"name":"Jochen"},{"name":"Paul"}]}' }- Parameters:
- coll- a collection
- c- a closure used to convert the objects of coll
- Throws:
- java.io.IOException
 
- 
callpublic void call(java.lang.String name, java.util.Collection coll, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c) throws java.io.IOExceptionDelegates tocall(String, Iterable, Closure)- Throws:
- java.io.IOException
 
- 
callpublic void call(java.lang.String name, java.util.Map map, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure callable) throws java.io.IOExceptionIf 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"}}' }- Parameters:
- name- The name of the JSON object
- map- The attributes of the JSON object
- callable- Additional attributes of the JSON object represented by the closure
- Throws:
- java.io.IOException
 
- 
invokeMethodpublic 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 classical builder-style: new StringWriter().with { wOr alternatively with a method call taking named arguments:->def json = new groovy.json.StreamingJsonBuilder(w) json.person { name "Tim" age 28 } assert w.toString() == '{"person":{"name":"Tim","age":28}}' }new StringWriter().with { wIf 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.->def json = new groovy.json.StreamingJsonBuilder(w) json.person name: "Tim", age: 32 assert w.toString() == '{"person":{"name":"Tim","age":32}}' }new StringWriter().with { wThe empty args call will create a key whose value will be an empty JSON object:->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"}}' }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
- Returns:
- the result of invoking the method
 
 
-