Groovy - Convert object to JSON string

groovy, json

Solution

Do you mean like:

import groovy.json.*

class Me {
    String name
}

def o = new Me( name: 'tim' )

println new JsonBuilder( o ).toPrettyString()

Problem

I'm pretty used to Grails converters, where you can convert any object to a JSON representation just like this (http://grails.org/Converters+Reference) ``` return foo as JSON ``` But in plain groovy, I cannot find an easy way to do this (http://groovy-lang.org/json.html) ``` JSONObject.fromObject(this) ``` return empty json strings... Am I missing an obvious Groovy converter ? Or should I go for jackson or gson library ?

Original source