public class JsonBuilder extends GroovyObjectSupport
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Constructor and description |
---|
JsonBuilder
() Instantiates a JSON builder. |
JsonBuilder
(Object content) Instantiates a JSON builder with some existing data structure. |
Type | Name and description |
---|---|
Object |
call(Map m) Named arguments can be passed to the JSON builder instance to create a root JSON object |
Object |
call(List l) A list of elements as arguments to the JSON builder creates a root JSON array |
Object |
call(Object... args) Varargs elements as arguments to the JSON builder create a root JSON array |
Object |
call(Collection 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(Closure c) A closure passed to a JSON builder will create a root JSON object |
Object |
getContent() |
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. |
String |
toPrettyString() Pretty-prints and formats the JSON payload. |
String |
toString() Serializes the internal data structure built with the builder to a conformant JSON payload string |
Writer |
writeTo(Writer out) The JSON builder implements the Writable interface,
so that you can have the builder serialize itself the JSON payload to a writer. |
Methods inherited from class | Name |
---|---|
class GroovyObjectSupport |
getMetaClass, getProperty, invokeMethod, setMetaClass, setProperty |
class Object |
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll |
Instantiates a JSON builder.
Instantiates a JSON builder with some existing data structure.
content
- a pre-existing data structureNamed arguments can be passed to the JSON builder instance to create a root JSON object
Example:
def json = new JsonBuilder()
json name: "Guillaume", age: 33
assert json.toString() == '{"name":"Guillaume","age":33}'
m
- a map of key / value pairsA list of elements as arguments to the JSON builder creates a root JSON array
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 valuesVarargs elements as arguments to the JSON builder create a root JSON array
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 valuesA collection and closure passed to a JSON builder will create a root JSON array applying the closure to each object in the collection
Example:
class Author {
String name
}
def authors = [new Author (name: "Guillaume"), new Author (name: "Jochen"), new Author (name: "Paul")]
def json = new JsonBuilder()
json authors, { Author author ->
name author.name
}
assert json.toString() == '[{"name":"Guillaume"},{"name":"Jochen"},{"name":"Paul"}]'
coll
- a collectionc
- a closure used to convert the objects of collA closure passed to a JSON builder will create a root JSON object
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 objectA 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:
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 keyPretty-prints and formats the JSON payload.
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.
Serializes the internal data structure built with the builder to a conformant JSON payload string
Example:
def json = new JsonBuilder()
json { temperature 37 }
assert json.toString() == '{"temperature":37}'
The JSON builder implements the 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 payloadCopyright © 2003-2015 The Apache Software Foundation. All rights reserved.