Package groovy.json
Class StreamingJsonBuilder.StreamingJsonDelegate
java.lang.Object
groovy.lang.GroovyObjectSupport
groovy.json.StreamingJsonBuilder.StreamingJsonDelegate
- All Implemented Interfaces:
GroovyObject
- Enclosing class:
- StreamingJsonBuilder
The delegate used when invoking closures
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanIndicates whether the next entry is the first one in the current context.protected groovy.json.StreamingJsonBuilder.StreamingJsonDelegate.StateTracks whether the next token should be a JSON name or value.protected final WriterWriter receiving the streamed JSON output. -
Constructor Summary
ConstructorsConstructorDescriptionStreamingJsonDelegate(Writer w, boolean first) Creates a delegate backed by the default generator.StreamingJsonDelegate(Writer w, boolean first, JsonGenerator generator) Creates a delegate backed by the supplied generator. -
Method Summary
Modifier and TypeMethodDescriptionvoidcall(String name, JsonOutput.JsonUnescaped json) Writes an unescaped value.voidWrites the name and another JSON objectvoidWrites the given Writable as the value of the given attribute namevoidA collection and closure passed to a JSON builder will create a root JSON array applying the closure to each object in the collectionvoidWrites the name and value of a JSON attributevoidWrites the name and a JSON arrayvoidWrites the name and value of a JSON attributevoidcall(String name, Collection coll, Closure c) Delegates tocall(String, Iterable, Closure)voidWrites the name and a JSON arraystatic voidClones a closure, assigns this delegate model, and writes its JSON content.static voidcloneDelegateAndGetContent(Writer w, Closure c, boolean first) Clones a closure, assigns this delegate model, and writes its JSON content.static voidcurryDelegateAndGetContent(Writer w, Closure c, Object o) Curries an object into a closure, assigns this delegate model, and writes its JSON content.static voidcurryDelegateAndGetContent(Writer w, Closure c, Object o, boolean first) Curries an object into a closure, assigns this delegate model, and writes its JSON content.invokeMethod(String name, Object args) Handles builder-style method calls for nested JSON output.static booleanisCollectionWithClosure(Object[] args) Indicates whether the supplied arguments represent a collection and closure pair.protected voidVerifies that the current output state expects a JSON value next.protected voidwriteArray(List<Object> list) Writes a JSON array value.static ObjectwriteCollectionWithClosure(Writer writer, Collection coll, Closure closure) Writes a JSON array by applying a closure to each element in the collection.protected voidWrites a JSON name and the following colon separator.protected voidwriteValue(Object value) Writes a scalar JSON value.Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClassMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface groovy.lang.GroovyObject
getProperty, setProperty
-
Field Details
-
writer
Writer receiving the streamed JSON output. -
first
protected boolean firstIndicates whether the next entry is the first one in the current context. -
state
protected groovy.json.StreamingJsonBuilder.StreamingJsonDelegate.State stateTracks whether the next token should be a JSON name or value.
-
-
Constructor Details
-
StreamingJsonDelegate
Creates a delegate backed by the default generator.- Parameters:
w- the writer receiving JSON outputfirst- whether the next entry is the first entry
-
StreamingJsonDelegate
Creates a delegate backed by the supplied generator.- Parameters:
w- the writer receiving JSON outputfirst- whether the next entry is the first entrygenerator- the generator used to serialize scalar values
-
-
Method Details
-
getWriter
- Returns:
- Obtains the current writer
-
invokeMethod
Handles builder-style method calls for nested JSON output.- Parameters:
name- the JSON name being writtenargs- the value, values, or closure associated with the name- Returns:
- this delegate
-
call
Writes the name and a JSON array- Parameters:
name- The name of the JSON attributelist- The list representing the array- Throws:
IOException
-
call
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(value=StreamingJsonDelegate.class,strategy=1) 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(value=StreamingJsonDelegate.class,strategy=1) Closure c) throws IOException Delegates tocall(String, Iterable, Closure)- Throws:
IOException
-
call
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(value=StreamingJsonDelegate.class,strategy=1) 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(value=StreamingJsonDelegate.class,strategy=1) Closure value) throws IOException Writes the name and another JSON object- Parameters:
name- The attribute namevalue- The value- Throws:
IOException
-
call
Writes an unescaped value. Note: can cause invalid JSON if passed JSON is invalid- Parameters:
name- The attribute namejson- The value- Throws:
IOException
-
call
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()Verifies that the current output state expects a JSON value next. -
writeName
Writes a JSON name and the following colon separator.- Parameters:
name- the JSON name to write- Throws:
IOException- if writing fails
-
writeValue
Writes a scalar JSON value.- Parameters:
value- the value to write- Throws:
IOException- if writing fails
-
writeArray
Writes a JSON array value.- Parameters:
list- the list to serialize- Throws:
IOException- if writing fails
-
isCollectionWithClosure
Indicates whether the supplied arguments represent a collection and closure pair.- Parameters:
args- the arguments to inspect- Returns:
trueif the arguments are a collection plus closure pair
-
writeCollectionWithClosure
public static Object writeCollectionWithClosure(Writer writer, Collection coll, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure closure) throws IOException Writes a JSON array by applying a closure to each element in the collection.- Parameters:
writer- the destination writercoll- the collection to serializeclosure- the closure used to build each JSON object entry- Returns:
- the supplied writer
- Throws:
IOException- if writing fails
-
cloneDelegateAndGetContent
public static void cloneDelegateAndGetContent(Writer w, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c) Clones a closure, assigns this delegate model, and writes its JSON content.- Parameters:
w- the destination writerc- the closure describing the JSON object
-
cloneDelegateAndGetContent
public static void cloneDelegateAndGetContent(Writer w, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c, boolean first) Clones a closure, assigns this delegate model, and writes its JSON content.- Parameters:
w- the destination writerc- the closure describing the JSON objectfirst- whether the next entry is the first entry
-
curryDelegateAndGetContent
public static void curryDelegateAndGetContent(Writer w, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c, Object o) Curries an object into a closure, assigns this delegate model, and writes its JSON content.- Parameters:
w- the destination writerc- the closure describing the JSON objecto- the object to curry into the closure
-
curryDelegateAndGetContent
public static void curryDelegateAndGetContent(Writer w, @DelegatesTo(value=StreamingJsonDelegate.class,strategy=1) Closure c, Object o, boolean first) Curries an object into a closure, assigns this delegate model, and writes its JSON content.- Parameters:
w- the destination writerc- the closure describing the JSON objecto- the object to curry into the closurefirst- whether the next entry is the first entry
-