9.1.6.2 Registering Custom Objects Marshallers 3.0.10

See the light - agile, industrial strength, rapid web application development made easy

(Quick Reference) # 9.1.6.2 Registering Custom Objects Marshallers - Reference Documentation Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari Version: 3.0.10 ## 9.1.6.2 Registering Custom Objects Marshallers Grails' Converters feature the notion of an ObjectMarshaller and each type can have a registered `ObjectMarshaller`. You can register custom `ObjectMarshaller` instances to completely customize response rendering. For example, you can define the following in `BootStrap.init`:
    XML.registerObjectMarshaller Book, { Book book, XML xml ->
      xml.attribute 'id', book.id
      xml.build {
        title(book.title)
      }
    }
You can customize the formatting of an individual value this way too. For example the JodaTime plugin does the following to support rendering of JodaTime dates in JSON output:
    JSON.registerObjectMarshaller(DateTime) {
        return it?.toString("yyyy-MM-dd'T'HH:mm:ss'Z'")
    }
In the case of JSON it's often simple to use a map to customize output:
    JSON.registerObjectMarshaller(Book) {
      def map= [:]
      map['titl'] = it.title
      map['auth'] = it.author
      return map
    }
#### Registering Custom Marshallers via Spring Note that if you have many custom marshallers it is recommended you split the registration of these into a separate class:

    class CustomMarshallerRegistrar {    @javax.annotation.PostConstruct
        void registerMarshallers() {
            JSON.registerObjectMarshaller(DateTime) {
                return it?.toString("yyyy-MM-dd'T'HH:mm:ss'Z'")
            }
        }
    }
Then define this class as Spring bean in `grails-app/conf/spring/resources.groovy`:
    beans = {
        myCustomMarshallerRegistrar(CustomMarshallerRegistrar)
    }
The `PostConstruct` annotation will get triggered on startup of your application. | Quick Reference (hide)