dukeofgaming
dukeofgaming

Reputation: 3198

render as JSON not working in Grails

I am fairly new to Groovy and Grails and I am trying to create a method that returns a JSON formatted string.

I read a bit about Converters, and from what I can understand from a couple of sources(here and here) I should be able to do this:

import grails.converters.JSON

class Record {

    //...

    private Map _metadata   = [:]

    String getMetadataJSON(){
        return render _metadata as JSON
    }
}

Not only this does not work, but the "render" keyword is not being resolved.

So my two questions are:

Upvotes: 0

Views: 982

Answers (1)

Joshua Moore
Joshua Moore

Reputation: 24776

render is used in a Controller within Grails and not (what appears to be) a Domain Class (in your example). If you want to get a JSON representation of something then simply:

String getMetadataJSON() {
  (_metadata as JSON)
}

The above will return a String representation in JSON format. Groovy doesn't require the return keyword.

Upvotes: 3

Related Questions