Vasil Koicev
Vasil Koicev

Reputation: 57

Grails GSON JSON views

I want to generate JSON output similar to this one:

var json = {
        id: "1",
        name: "AAA",
        children: [{
            id: "2",
            name: "BBB",
            data: {
                relation: "<b>Connections:</b><ul><li> AAA <div>(relation: direct)</div></li><li> CCC <div>(relation: direct)</div></li></ul>"
            },
            children: [{
                id: "3",
                name: "CCC",
                data: {
                    relation: "<b>Connections:</b><ul><li> BBB <div>(relation: direct)</div></li></ul>"
                },
                children: []
            }]
        }, ....

Here is what I did so far:

grails create-domain-resource json.Object

class Object{
    String name
    String relation
    static hasMany = [children: Object]

    public String getData() {
        def writer = new StringWriter()

_object.gson


//json g.render(hero)
json {
    //data hero.data
    data: {relation hero.relation}
    name hero.name
}

Problem I'm not able to produce:

 data: {
                    relation: "<b>Connections:</b><ul><li> BBB <div>(relation: direct)</div></li></ul>"
                },

Questions: 1) I've read the official documentation but I'm not able to find how to do transient fields 2) What is best approach of mixing json and xml / html. 3) How to pass json code to another view variable

thank you in advance

Upvotes: 2

Views: 3315

Answers (1)

DataScientYst
DataScientYst

Reputation: 442

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.Object

model {
    Object object
}

json tmpl.object(object)

Template 2 _object.gson

import json.Object

model {
    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.JsonViewTemplateEngine
import 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 **

Upvotes: 4

Related Questions