Hi, I'm POSTing json data to a Grails controller which I then parse using JSON.parse. It all works fine except for date fields. I don't believe there is an explicit syntax in json to represent a Date but I've tried a number of formats with no luck. Can anyone tell me what format I should use so that the grails JSON parser can create a Date object.
+1
A:
There isn't a specific format, but you can define your own. For example, these guys here are adding a '@' to the beginning and the end of the string.
According to Grails docs here, you can define:
grails.converters.json.date (String) - Configure how Date values are serialized to JSON
- "default" - String representation according to the JSON specification
- "javascript" - new Date(...)
Update: It appears that there is no mapping to Java Date objects. If you know the fields that are dates, you can parse them into Dates
NullUserException
2010-07-26 13:23:22
Thanks but that doesn't help me. I'm looking to understand how the Grails framework parses JSON to produce Groovy/Java Date Objects. I haven't found any documentation on how this works or how to define my own.
subcontact
2010-07-26 13:26:53
@subcon Read my edit
NullUserException
2010-07-26 13:34:03
Thanks.. That info is specifically about how Grails generates JSON rather than parsing incoming JSON. I have tried against the ISO_8601 format (EG 2010-07-26T12:36Z) and that still is left as a String instead of a Date.
subcontact
2010-07-26 13:52:44
@sub It appears that there is no [mapping](http://code.google.com/p/json-simple/wiki/MappingBetweenJSONAndJavaEntities) to Java Date objects. If you know the fields that are dates, you can [parse](http://java.sun.com/j2se/1.4.2/docs/api/java/text/DateFormat.html#parse) them into Dates.
NullUserException
2010-07-26 14:15:26