|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | PROPERTY | CONSTR | METHOD | DETAIL: FIELD | PROPERTY | CONSTR | METHOD |
java.lang.Object groovy.json.JsonBuilder
class JsonBuilder
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.
Example:
def builder = new groovy.json.JsonBuilder()
def root = builder.people {
person {
firstName 'Guillame'
lastName 'Laforge'
// Named arguments are valid values for objects too
address(
city: 'Paris',
country: 'France',
zip: 12345,
)
married true
// a list of values
conferences 'JavaOne', 'Gr8conf'
}
}
// creates a data structure made of maps (Json object) and lists (Json array)
assert root instanceof Map
assert builder.toString() == '{"people":{"person":{"firstName":"Guillame","lastName":"Laforge","address":{"city":"Paris","country":"France","zip":12345},"married":true,"conferences":["JavaOne","Gr8conf"]}}}'
Property Summary | |
---|---|
java.lang.Object |
content
Resulting data structure made of lists and maps, representing a JSON payload |
Constructor Summary | |
JsonBuilder(java.lang.Object content = null)
Instanciates 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. |
java.lang.String
|
toPrettyString()
Pretty-prints and formats the JSON payload. |
java.lang.String
|
toString()
Serializes the internal data structure built with the builder to a conformant JSON payload string |
java.io.Writer
|
writeTo(java.io.Writer out)
The JSON builder implements the |
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 |
---|
java.lang.Object content
Constructor Detail |
---|
JsonBuilder(java.lang.Object content = null)
content
- a pre-existing data structure, default to null
Method Detail |
---|
java.lang.Object call(java.util.Map m)
Example:
def json = new JsonBuilder()
json name: "Guillaume", age: 33
assert json.toString() == '{"name":"Guillaume","age":33}'
m
- a map of key / value pairs
java.lang.Object call(java.util.List l)
Example:
def json = new JsonBuilder()
def result = json([1, 2, 3])
assert result instanceof List
assert json.toString() == "[1,2,3]"
l
- a list of values
java.lang.Object call(java.lang.Object... args)
Example:
def json = new JsonBuilder()
def result = json 1, 2, 3
assert result instanceof List
assert json.toString() == "[1,2,3]"
args
- an array of values
java.lang.Object call(Closure c)
Example:
def json = new JsonBuilder()
def result = json {
name "Guillaume"
age 33
}
assert result instanceof Map
assert json.toString() == '{"name":"Guillaume","age":33}'
c
- a closure whose method call statements represent key / values of a JSON object
java.lang.Object invokeMethod(java.lang.String name, java.lang.Object args)
Example with a classicala builder-style:
def json = new JsonBuilder()
def result = json.person {
name "Guillaume"
age 33
}
assert result instanceof Map
assert json.toString() == '{"person":{"name":"Guillaume","age":33}}'
Or alternatively with a method call taking named arguments:
def json = new JsonBuilder()
json.person name: "Guillaume", age: 33
assert json.toString() == '{"person":{"name":"Guillaume","age":33}}'
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.
def json = new JsonBuilder()
json.person(name: "Guillaume", age: 33) { town "Paris" }
assert json.toString() == '{"person":{"name":"Guillaume","age":33,"town":"Paris"}}'
The empty args call will create a key whose value will be an empty JSON object:
def json = new JsonBuilder()
json.person()
assert json.toString() == '{"person":{}}'
name
- the single keyargs
- the value associated with the key
java.lang.String toPrettyString()
This method calls the JsonLexer to parser the output of the builder, so this may not be an optimal method to call, and should be used mainly for debugging purpose for a human-readable output of the JSON content.
java.lang.String toString()
Example:
def json = new JsonBuilder()
json { temperature 37 }
assert json.toString() == '{"temperature":37}'
java.io.Writer writeTo(java.io.Writer out)
Writable
interface,
so that you can have the builder serialize itself the JSON payload to a writer.
Example:
def json = new JsonBuilder()
json { temperature 37 }
def out = new StringWriter()
out << json
assert out.toString() == '{"temperature":37}'
out
- a writer on which to serialize the JSON payload
Groovy Documentation