@groovy.transform.Trait @groovy.transform.CompileStatic trait JsonView extends GrailsView
Extends default view API with additional methods
Modifiers | Name | Description |
---|---|---|
private JsonGenerator |
generator |
The default generator |
private HalViewHelper |
hal |
The HAL view helper |
private StreamingJsonBuilder |
json |
The StreamingJsonBuilder instance |
private JsonApiIdRenderStrategy |
jsonApiIdRenderStrategy |
The strategy to use to render identifiers in the JSON API specification |
private JsonApiViewHelper |
jsonapi |
The JSON API view helper |
private java.util.Collection<ParentInfo> |
parentData |
|
private TemplateRenderer |
tmpl |
The template namespace |
private GrailsJsonViewHelper |
viewHelper |
Overrides the default helper with new methods specific to JSON building |
Fields inherited from class | Fields |
---|---|
trait GrailsView |
actionName, config, controllerName, controllerNamespace, prettyPrint, viewHelper, viewTemplate |
trait HttpView |
params, request, response |
trait View |
locale, out |
Type | Name and description |
---|---|
JsonGenerator |
generator The default generator |
HalViewHelper |
hal The HAL view helper |
StreamingJsonBuilder |
json The StreamingJsonBuilder instance |
JsonApiIdRenderStrategy |
jsonApiIdRenderStrategy The strategy to use to render identifiers in the JSON API specification |
JsonApiViewHelper |
jsonapi The JSON API view helper |
java.util.Collection<ParentInfo> |
parentData |
TemplateRenderer |
tmpl The template namespace |
Type Params | Return Type | Name and description |
---|---|---|
|
GrailsJsonViewHelper |
getG()
|
|
void |
inherits(java.util.Map arguments) Specify a template that this template inherits from |
|
java.lang.Object |
json(java.util.Map m) Output JSON for the given map |
|
void |
json(java.lang.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() |
|
java.lang.Object |
json(java.util.List l) A list of elements as arguments to the JSON builder creates a root JSON array |
|
java.lang.Object |
json(java.lang.Object[] args) Varargs elements as arguments to the JSON builder create a root JSON array |
|
java.lang.Object |
json(java.lang.Iterable coll, groovy.lang.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 |
|
java.lang.Object |
json(groovy.lang.Closure c) A closure passed to a JSON builder will create a root JSON object |
|
void |
json(java.lang.String name, groovy.lang.Closure c) A name and a closure passed to a JSON builder will create a key with a JSON object |
|
void |
json(java.lang.String name, java.lang.Iterable coll, groovy.lang.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 |
json(java.lang.String name, java.util.Map map, groovy.lang.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. |
Methods inherited from class | Name |
---|---|
trait GrailsView |
getG, getLinkGenerator, getMappingContext, getMessageSource, getMimeUtility, getProxyHandler, getTemplateEngine, model |
trait HttpView |
getPage |
The default generator
The HAL view helper
The StreamingJsonBuilder instance
The strategy to use to render identifiers in the JSON API specification
The JSON API view helper
The template namespace
Overrides the default helper with new methods specific to JSON building
The default generator
The HAL view helper
The StreamingJsonBuilder instance
The strategy to use to render identifiers in the JSON API specification
The JSON API view helper
The template namespace
Specify a template that this template inherits from
arguments
- The argumentsOutput JSON for the given map
m
- The JSON mapThe 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":{}}' }
name
- The name of the empty object to createA list of elements as arguments to the JSON builder creates a root JSON array
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 valuesVarargs elements as arguments to the JSON builder create a root JSON array
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 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")] 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 collA closure passed to a JSON builder will create a root JSON object
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 objectA name and a closure passed to a JSON builder will create a key with a JSON object
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 objectA 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
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 collIf 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"}}' }
name
- The name of the JSON objectmap
- The attributes of the JSON objectcallable
- Additional attributes of the JSON object represented by the closure