I have a domain class Person in my grails application that I need to output in different forms (of JSON) depending on the context.
In one context, I need to render only a couple of fields (say id and name). In another context, I want to render a lot more (id, name, credentials, age etc..). I'm wondering if it is possible to unregister a particular marshaller right after use.
Essentially what I'm looking for is something like:
-------------------------------------------------------------
// context #1
JSON.registerObjectMarshaller(Person) {
... output just id and name
}
render myPerson as JSON
JSON.unregisterObjectMarshaller(Person) // how do i do this?
-------------------------------------------------------------
// context #2
JSON.registerObjectMarshaller(Person) {
... output all fields
}
render myPerson as JSON
JSON.unregisterObjectMarshaller(Person) // how do i do this?
-------------------------------------------------------------
Note: I can create 2 empty subclasses for Person and then have separate Marshallers registered for each. As the number of contexts increase the number of dummy subclasses will also go up. This is very unclean imo.