Grails GSON JSON views Grails GSON JSON views json json

Grails GSON JSON views


You can check the official documentation for gson views at:

http://views.grails.org/latest/#_json_view_api

Domain class

class Object{    String name    String relation    static hasMany = [children: Object]    public String getRelation() {

Template 1 object.gson

import json.Objectmodel {    Object object}json tmpl.object(object)

Template 2 _object.gson

import json.Objectmodel {    Object object}json {    id object.id    data(relation: object.relation)    name object.name    children g.render(object.children,[ excludes:['exclude_fields']])    //children g.render(object.children,[resolveTemplate: false]) // one to many relations - avoid circular error    //object2 object.book.name // one to one relations}

ObjectController

import grails.plugin.json.view.JsonViewTemplateEngineimport org.springframework.beans.factory.annotation.Autowired    @Autowired    JsonViewTemplateEngine templateEngine    def test() {        def t = templateEngine.resolveTemplate('/object/object')        def writable = t.make(object: Object.get(params.id))        def sw = new StringWriter()        writable.writeTo( sw )        return [json:sw]    }

Questions:

1) I've read the official documentation but I'm not able to find how to do transient fields - you can use Named arguments which are valid values for objects or getters

2) What is best approach of mixing json and xml / html. - **I guess you can check : http://docs.groovy-lang.org/latest/html/gapi/groovy/json/StreamingJsonBuilder.html **

3) How to pass json code to another view variable - **check code above at objectController **