Package groovy.json
Class StreamingJsonBuilder.StreamingJsonDelegate
- java.lang.Object
-
- groovy.lang.GroovyObjectSupport
-
- groovy.json.StreamingJsonBuilder.StreamingJsonDelegate
-
- All Implemented Interfaces:
GroovyObject
- Enclosing class:
- StreamingJsonBuilder
public static class StreamingJsonBuilder.StreamingJsonDelegate extends GroovyObjectSupport
The delegate used when invoking closures
-
-
Constructor Summary
Constructors Constructor Description StreamingJsonDelegate(Writer w, boolean first)
StreamingJsonDelegate(Writer w, boolean first, JsonGenerator generator)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
call(String name, JsonOutput.JsonUnescaped json)
Writes an unescaped value.void
call(String name, Closure value)
Writes the name and another JSON objectvoid
call(String name, Writable json)
Writes the given Writable as the value of the given attribute namevoid
call(String name, 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 collectionvoid
call(String name, Object value)
Writes the name and value of a JSON attributevoid
call(String name, Object... array)
Writes the name and a JSON arrayvoid
call(String name, Object value, Closure callable)
Writes the name and value of a JSON attributevoid
call(String name, Collection coll, Closure c)
Delegates tocall(String, Iterable, Closure)
void
call(String name, List<Object> list)
Writes the name and a JSON arraystatic void
cloneDelegateAndGetContent(Writer w, Closure c)
static void
cloneDelegateAndGetContent(Writer w, Closure c, boolean first)
static void
curryDelegateAndGetContent(Writer w, Closure c, Object o)
static void
curryDelegateAndGetContent(Writer w, Closure c, Object o, boolean first)
Writer
getWriter()
Object
invokeMethod(String name, Object args)
Invokes the given method.static boolean
isCollectionWithClosure(Object[] args)
protected void
verifyValue()
protected void
writeArray(List<Object> list)
static Object
writeCollectionWithClosure(Writer writer, Collection coll, Closure closure)
protected void
writeName(String name)
protected void
writeValue(Object value)
-
Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, getProperty, setMetaClass, setProperty
-
-
-
-
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
-
invokeMethod
public Object invokeMethod(String name, Object args)
Description copied from interface:GroovyObject
Invokes the given method.- Specified by:
invokeMethod
in interfaceGroovyObject
- Overrides:
invokeMethod
in classGroovyObjectSupport
- Parameters:
name
- the name of the method to callargs
- the arguments to use for the method call- Returns:
- the result of invoking the method
-
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 attributelist
- 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 attributearray
- The list representing the array- Throws:
IOException
-
call
public void call(String name, Iterable coll, @DelegatesTo(StreamingJsonDelegate.class) 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 collectionExample:
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 collectionc
- a closure used to convert the objects of coll- Throws:
IOException
-
call
public void call(String name, Collection coll, @DelegatesTo(StreamingJsonDelegate.class) Closure c) throws IOException
Delegates tocall(String, Iterable, Closure)
- 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 namevalue
- The value- Throws:
IOException
-
call
public void call(String name, Object value, @DelegatesTo(StreamingJsonDelegate.class) Closure callable) throws IOException
Writes the name and value of a JSON attribute- Parameters:
name
- The attribute namevalue
- The value- Throws:
IOException
-
call
public void call(String name, @DelegatesTo(StreamingJsonDelegate.class) Closure value) throws IOException
Writes the name and another JSON object- Parameters:
name
- The attribute namevalue
- The value- Throws:
IOException
-
call
public void call(String name, JsonOutput.JsonUnescaped json) throws IOException
Writes an unescaped value. Note: can cause invalid JSON if passed JSON is invalid- Parameters:
name
- The attribute namejson
- The value- Throws:
IOException
-
call
public void call(String name, Writable json) throws IOException
Writes the given Writable as the value of the given attribute name- Parameters:
name
- The attribute namejson
- The writable value- Throws:
IOException
-
verifyValue
protected void verifyValue()
-
writeName
protected void writeName(String name) throws IOException
- Throws:
IOException
-
writeValue
protected void writeValue(Object value) throws IOException
- Throws:
IOException
-
writeArray
protected void writeArray(List<Object> list) throws IOException
- Throws:
IOException
-
isCollectionWithClosure
public static boolean isCollectionWithClosure(Object[] args)
-
writeCollectionWithClosure
public static Object writeCollectionWithClosure(Writer writer, Collection coll, @DelegatesTo(StreamingJsonDelegate.class) Closure closure) throws IOException
- Throws:
IOException
-
cloneDelegateAndGetContent
public static void cloneDelegateAndGetContent(Writer w, @DelegatesTo(StreamingJsonDelegate.class) Closure c)
-
cloneDelegateAndGetContent
public static void cloneDelegateAndGetContent(Writer w, @DelegatesTo(StreamingJsonDelegate.class) Closure c, boolean first)
-
curryDelegateAndGetContent
public static void curryDelegateAndGetContent(Writer w, @DelegatesTo(StreamingJsonDelegate.class) Closure c, Object o)
-
curryDelegateAndGetContent
public static void curryDelegateAndGetContent(Writer w, @DelegatesTo(StreamingJsonDelegate.class) Closure c, Object o, boolean first)
-
-