I use Grails 2.3.8. I'm noticing strange behavior in Grails JSON rendering. I have a domain class ShiftUpdateResponse
mapped to mongodb that looks as below.
class ShiftUpdateResponse {
List<Employee> employees
SalesPersonHours salesPersonHours;
Integer fromDate
Integer toDate
Integer success
static mapWith = "mongo"
static hasMany = [employees:Employee]
static embedded = ['employees','salesPersonHours']
static constraints = {
salesPersonHours nullable:true
success nullable:true
fromDate nullable:true
toDate nullable:true
}
String toString() {
" $employees"
}
}
When I create a ShiftUpdateResponse
object in my controller and render it as JSON using the snippet below:
def sur = new ShiftUpdateResponse(employees:[], salesPersonHours:[], success:1, fromDate:from, toDate:to)
println "from " + from
println "to " + to
println "from sur " + sur.fromDate
println "to sur " + sur.toDate
JSON.use('deep')
render sur as JSON
I notice the following things
when I do not save the created instance sur
before rendering, the console output is
from 20130624
to 20130623
from sur null
to sur null
when I do save the created instance before rendering, the console output is
from 20130624
to 20130623
from sur null
to sur 20130623
In either case, the JSON rendered does not contain the fields fromDate
and toDate
. What could be the reason ? (I do not need this instance to be persisted, I just need it to be rendered as a JSON).
EDIT:
The JSON rendered looks like :
{
"class": "com.scheduling.json.week.graphicalMode.ShiftUpdateResponse",
"id": 37,
"employees": [],
"salesPersonHours": null,
"success": 1
}
Controller code :
@Transactional
def updateShift(){
def reqJson = request.JSON
somService.updateShift(reqJson)
def from
def to
def week = WeekJson.get(reqJson.week_id)
// code to construct from and to from week
def sur = new ShiftUpdateResponse(employees:[],salesPersonHours:[],success:1,fromDate:from,toDate:to)
//sur.save(flush:true)
println "from "+from
println "to "+to
println "from sur "+sur.fromDate
println "to sur "+sur.toDate
JSON.use('deep')
render sur as JSON
}
Sorry,noob mistake. The problems might have been because, there was another copy of the same file in a different package. Deleting the duplicate fixed things up.