You'd probably want to use what's called a named config instead of 'swapping' marshallers. You can wrap this up into a neater class/utility, but somewhere (like 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 a controller, you can choose which style of marshalled 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
}