4

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.

subcontact
  • 201
  • 1
  • 5
  • This question has some time, but well here is an answer that I think solves your problem. http://stackoverflow.com/questions/963922/grails-date-unmarshalling – rsilva4 Feb 11 '12 at 17:06

1 Answers1

1

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

Al Belsky
  • 1,544
  • 13
  • 15
NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • 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 Jul 26 '10 at 13:26
  • 1
    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 Jul 26 '10 at 13:52
  • @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 Jul 26 '10 at 14:15