////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////

Grails' Converters feature the notion of an link:{api}org/grails/web/converters/marshaller/ObjectMarshaller.html[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`:

[source,groovy]
----
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 https://grails-plugins.github.io/grails-joda-time/index.html[JodaTime plugin] does the following to support rendering of JodaTime dates in JSON output:

[source,groovy]
----
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:

[source,groovy]
----
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:

[source,groovy]
----
class CustomMarshallerRegistrar {

    @jakarta.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`:

[source,groovy]
----
beans = {
    myCustomMarshallerRegistrar(CustomMarshallerRegistrar)
}
----

The `PostConstruct` annotation will get triggered on startup of your application.
