Grails: How to dump an object's properties?

Best possible option would be to render the object graph as deep JSON or XML (cannot achieve with normal JSON or XML converters). Grails provides converters which effectively parses the object graph to a human readable form and is also useful for rendering response in webservice calls. Below is a detailed example as to how an object graph can be rendered either to a valid JSON Object or an XML.

Use Case:
Consider an object graph like:

Parent has Child, name, dob, age
Child has name, dob, age, GrandChild and has many Qualities
GrandChild has Address , blah, blah etc

Sample
Detail example using domain objects with outputs can be found here.
Detail example using POGOs with outputs can be found here

Key Area:
The key area to focus on is the controller method where all Parents are fetched and serialized as JSON or XML

//Parent Controller
import grails.converters.JSON
import grails.converters.XML
def index() {
        JSON.use('deep')
        render Parent.all as JSON

        //XML.use('deep')
        //render Parent.all as XML
    }

Inference:
Grails converters can also be used for normal POGO object graphs and are not only confined to domain objects. You can either write the response to an out stream, log appender or to an http response, you could achieve the deep serialization of the object graph from root to leaf node in all cases.

The example mentioned in above would make it clear as to how the out format would look like.

Sample apps written and tested using Grails 2.2.2.


You can try use to render the object as JSON instead of using making it implicitly call toString(). I think it will render the structure of the object properly.