Search code examples
jsongrailsgroovy

How to import default Grails tag library into JSON Views


The new features for Grails JSON Views are great, but it would be super handy to access all the grails tag libraries in JSON Views. For instance I would like to access the formatDate tag for formatting my dateOfBirth field like so:

model {
    Person person
}
json {
    id person.id
    displayName person.displayName
    dateOfBirth g.formatDate(date: person?.dateOfBirth, format: 'dd-MM-yyyy')
}

The benefit of using the formatDate tag is that I can leverage the default date format and also import date format properties without extraneous code.

I am using Grails 3.2.0 and Views 1.1.0.


Solution

  • Tag libraries are a feature of GSP, which is view rendering technology designed primarily to render markup defined as tags (HTML, XML etc.).

    JSON views are designed to render JSON, hence support for tag libraries (i.e. tags) makes no sense, since tags don't exist in JSON.