DJ180
DJ180

Reputation: 19854

Grails: Converting non domain class to JSON

I'm attempting to convert my Map<String, TableEntry> to JSON in my controller as follows

def index() {
        // get tables
        JSON.use('deep')
        render(tables) as JSON
    }

My TableEntry is a non-domain class as I do not wish to persist it

class TableEntry {
    String teamName
    Integer gamesPlayed = 0
    Integer gamesWon = 0
    Integer gamesDrawn = 0
    Integer gamesLost = 0
    Integer points = 0

    // other methods

However, when my JSON is rendered in the client, I get the following:

'Team A':TableEntry@3b52fb28, 'Team Z':TableEntry@44e71d85

How do I get this to convert fully?

Upvotes: 2

Views: 270

Answers (1)

th3morg
th3morg

Reputation: 4779

Your render statement is incorrect. You have:

    render(tables) as JSON

However, it should read:

    render tables as JSON

By wrapping the variable "tables" in parenthesis, the render is happening before you can cast "tables" to JSON.

Upvotes: 1

Related Questions