"how to set date format for json converter in grails" Code Answer

4

i generally use a custom marshaller. assume you have the following domain

class address { 
  string addressone
  string city
  //bla bla
  date datecreated
}

create a class under src/groovy like this

class addressmarshaller {
  void register() {
     json.registerobjectmarshaller(address) { address address ->
      return [ 
         id: address.id,
         addressone: address.addressone,
         city: address.city,
         datecreated: address.datecreated.format('yyyy-mm-dd')
      ]
  }
}

then in your bootstrap file do the following:

[ new addressmarshaller() ].each { it.register() }

the reason i do it as an array like that is because i have multiple marshallers.

now, anytime you do address as json, you'll get the json you've described and you're correct date format. i know this seems like overkill for formatting a date in json but this has a lot of other benefits.

By Chris Hawkins on July 26 2022

Answers related to “how to set date format for json converter in grails”

Only authorized users can answer the Search term. Please sign in first, or register a free account.