You probably want to use the so-called named config instead of swapping marshalers. You can wrap this in a neater class / utility, but somewhere (e.g. Bootstrap.groovy) do:
JSON.createNamedConfig('thin') { it.registerObjectMarshaller( Person ) { Person person -> return [ id: person.id, name: person.name, ] } } JSON.createNamedConfig('full') { it.registerObjectMarshaller( Person ) { Person person -> return [ id: person.id, name: person.name, age: person.age ] } }
Then, in the controller, you can choose which style of marshalling person to show:
// Show lots of stuff JSON.use('full') { render people as JSON }
or
// Show less stuff JSON.use('thin') { render people as JSON }
source share