////
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

https://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.
////

JSON views are written in Groovy, end with the file extension `gson` and reside in the `grails-app/views` directory.  They allow rendering of JSON responses using Groovy's {groovyapi}groovy/json/StreamingJsonBuilder.html[StreamingJsonBuilder] by providing a DSL for producing output in the JSON format. A hello world example can be seen below:

[source,groovy]
.`grails-app/views/hello.gson`
json.message {
    hello "world"
}

The above JSON view results in the output:

[source,javascript]
{"message":{ "hello":"world"}}

The `json` variable is an instance of {groovyapi}groovy/json/StreamingJsonBuilder.html[StreamingJsonBuilder]. See the {groovydocumentation}core-domain-specific-languages.html#_streamingjsonbuilder[documentation] in the Groovy user guide for more information on `StreamingJsonBuilder`.

More example usages:

[source,groovy]
json(1,2,3) == "[1,2,3]"
json { name "Bob" } == '{"name":"Bob"}'
json([1,2,3]) { n it } == '[{"n":1},{"n":2},{"n":3}]'


You can specify a model to view in one of two ways. Either with a `model` block:

[source,groovy]
.`grails-app/views/hello.gson`
model {
    String message
}
json.message {
    hello message
}

Or with the `@Field` transformation provided by Groovy:

[source,groovy]
.`grails-app/views/hello.gson`
import groovy.transform.Field
@Field String message
json.message {
    hello message
}
