In Grails, you can use the JSON converters to do this in the controller:
render Book.list() as JSON
The render result is
[
{"id":1,
"class":"Book",
"author":"Stephen King",
"releaseDate":'2007-04-06T00:00:00',
"title":"The Shining"}
]
You can control the output date by make a setting in Config.groovy
grails.converters.json.date = 'javascript' // default or javascipt
Then the result will be a navtive javascript date
[
{"id":1,
"class":"Book",
"author":"Stephen King",
"releaseDate":new Date(1194127343161),
"title":"The Shining"}
]
If I want to get a specific date format like this:
"releaseDate":"06-04-2007"
I have to do 'collect'
return Book.list().collect(){
[
id:it.id,
class:it.class,
author:it.author,
releaseDate:new java.text.SimpleDateFormat("dd-MM-yyyy").format(it.releaseDate),
title:it.title
]
} as JSON
which requires a lot of typing. Is there a simpler way to do it?