java - Grails XML marshalling: change default "<list>" root element name -


by default grails renders list in xml <list> element tag @ root. likewise renders map <map>. control name of root element.

if i'm returning arraylist of user, i'd see:

<users>     <user>...</user>     <user>...</user> </users> 

how can achieve above? here requirements:

  • easy apply serialization 50+ domain classes
  • abstracted developers no explicit coding required during rendering domain objects (i.e., when render() or respond() invoked, arraylist still passed in, no explicit casting/converting as mynewtype)
  • able handle edge case of empty list (should return <users/>)

nice-to-haves:

  • if formula can applied map well, great :)

i have been semi-successful in achieving goals above, except don't know how account empty list case. implemented own objectmarshaller renders objects of type list. long list contains 1 element, can check element's type , determine plural tag name should (user => users). if list empty, , since java generics erasure (unless that's different in groovy?) have no way name empty list other defaulting <list/>, not acceptable.

some resources i've been through:

a way achieve write subclass collectionmarshaller class , register in our grails application. can example register custom implementation in bootstrap.groovy following code:

import org.codehaus.groovy.grails.web.converters.marshaller.xml.collectionmarshaller import grails.converters.xml  class bootstrap {    def init = { servletcontext ->     // register custom collection marshaller list user instances.     // root element name set users.     xml.registerobjectmarshaller(new collectionmarshaller() {         @override         public boolean supports(object object) {             object instanceof list<user>         }          @override         string getelementname(final object o) {             'users'         }     })   } } 

to make work more domain classes might reference domain classes in bootstrap.groovy , loop through them configure custom collectionmarshaller instances.

for maps can extend mapmarshaller

also described in http://mrhaki.blogspot.com/2014/02/grails-goodness-customize-root-element.html


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -