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"]}}}' }
Modifier and Type | Class and Description |
---|---|
static class |
StreamingJsonBuilder.StreamingJsonDelegate
The delegate used when invoking closures
|
Constructor and Description |
---|
StreamingJsonBuilder(Writer writer)
Instantiates a JSON builder.
|
StreamingJsonBuilder(Writer writer,
Object content)
Instantiates a JSON builder, possibly with some existing data structure.
|
Modifier and Type | Method and Description |
---|---|
Object |
call(Closure c)
A closure passed to a JSON builder will create a root JSON object
|
Object |
call(Collection coll,
Closure c)
Delegates to
call(Iterable, Closure) |
Object |
call(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
|
Object |
call(List l)
A list of elements as arguments to the JSON builder creates a root JSON array
|
Object |
call(Map m)
Named arguments can be passed to the JSON builder instance to create a root JSON object
|
Object |
call(Object... args)
Varargs elements as arguments to the JSON builder create a root JSON array
|
void |
call(String name)
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":{}}' } |
void |
call(String name,
Closure c)
A name and a closure passed to a JSON builder will create a key with a JSON object
|
void |
call(String name,
Collection coll,
Closure c)
Delegates to
call(String, Iterable, Closure) |
void |
call(String name,
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 collection
|
void |
call(String name,
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.
|
Object |
invokeMethod(String name,
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.
|
getMetaClass, getProperty, setMetaClass, setProperty
public StreamingJsonBuilder(Writer writer)
writer
- A writer to which Json will be writtenpublic StreamingJsonBuilder(Writer writer, Object content) throws IOException
writer
- A writer to which Json will be writtencontent
- a pre-existing data structure, default to nullIOException
public Object call(Map m) throws IOException
Example:
new StringWriter().with { w -> def json = new groovy.json.StreamingJsonBuilder(w) json name: "Tim", age: 31 assert w.toString() == '{"name":"Tim","age":31}' }
m
- a map of key / value pairsIOException
public void call(String name) throws IOException
new StringWriter().with { w -> def json = new groovy.json.StreamingJsonBuilder(w) json.person() assert w.toString() == '{"person":{}}' }
name
- The name of the empty object to createIOException
public Object call(List l) throws IOException
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]" }
l
- a list of valuesIOException
public Object call(Object... args) throws IOException
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]" }
args
- an array of valuesIOException
public Object call(Iterable coll, @DelegatesTo(value=StreamingJsonBuilder.StreamingJsonDelegate.class) Closure c) throws IOException
Example:
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"}]' }
coll
- a collectionc
- a closure used to convert the objects of collIOException
public Object call(Collection coll, @DelegatesTo(value=StreamingJsonBuilder.StreamingJsonDelegate.class) Closure c) throws IOException
call(Iterable, Closure)
IOException
public Object call(@DelegatesTo(value=StreamingJsonBuilder.StreamingJsonDelegate.class) Closure c) throws IOException
Example:
new StringWriter().with { w -> def json = new groovy.json.StreamingJsonBuilder(w) json { name "Tim" age 39 } assert w.toString() == '{"name":"Tim","age":39}' }
c
- a closure whose method call statements represent key / values of a JSON objectIOException
public void call(String name, @DelegatesTo(value=StreamingJsonBuilder.StreamingJsonDelegate.class) Closure c) throws IOException
Example:
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}}' }
name
- The key for the JSON objectc
- a closure whose method call statements represent key / values of a JSON objectIOException
public void call(String name, Iterable coll, @DelegatesTo(value=StreamingJsonBuilder.StreamingJsonDelegate.class) Closure c) throws IOException
Example:
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"}]}' }
coll
- a collectionc
- a closure used to convert the objects of collIOException
public void call(String name, Collection coll, @DelegatesTo(value=StreamingJsonBuilder.StreamingJsonDelegate.class) Closure c) throws IOException
call(String, Iterable, Closure)
IOException
public void call(String name, Map map, @DelegatesTo(value=StreamingJsonBuilder.StreamingJsonDelegate.class) Closure callable) throws IOException
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"}}' }
name
- The name of the JSON objectmap
- The attributes of the JSON objectcallable
- Additional attributes of the JSON object represented by the closureIOException
public Object invokeMethod(String name, Object args)
Example with a classical 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":{}}' }
invokeMethod
in interface GroovyObject
invokeMethod
in class GroovyObjectSupport
name
- the single keyargs
- the value associated with the key