AjaxPro JSON Parser and Dates

Because there are a lot of developers using the stand-alone JSON parser (which is included in the release package of AjaxPro) I decided today to serialize the .NET data type System.DateTime with the UniversalSortableDateTimePattern (yyyy'-'MM'-'dd HH':'mm':'ss'Z') as a simple string. You can use this string as input value, too, it will be parsed to an .NET System.DateTime.

Below you will find an example how to use the different JSON outputs in a simple ConsoleApplication:

JavaScriptSerializer.Serialize(DateTime.Now)  JavaScriptDeserializer.DeserializeFromJson<DateTime>(json)  UniversalSortableDateTimePattern 

The output will be similar to this:

"2006-09-22 09:49:18Z"
22.09.2006 09:49:18
22.10.2006 12:00:00
23.07.1977 03:13:49
20.09.2006 12:23:02
 

 Note: when using AjaxPro for ASP.NET web application you will still get the JavaScript Date object.

Published Friday, September 22, 2006 11:42 AM by Michael Schwarz

Comments

# re: AjaxPro JSON Parser and Dates

Friday, October 27, 2006 5:35 AM by Michael Rumpler

I don't use the AjaxPro-calls from the client directly but I serialize my JS-object with http://json.org/json.js. This doesn't work for Date objects so I have to send a string.

On the server I am using AjaxPro.dll and AjaxPro.JSON.dll, Version 6.10.6.2. Here I try to use AjaxPro.JavaScriptDeserializer.DeserializeFromJson to get my object.

But I couldn't get a string in the format "1973-02-01" or "1973-02-01 00:00:00000" to be deserialized into a DateTime object.

I work around that problem by sending the DateTime to the server as a "M/D/Y" string and do a DateTime.Parse() afterwards.

The Serialization doesn't work like you described either. A DateTime object is serialized into a "new Date(...)" call.

E.g.:

 AjaxPro.JavaScriptSerializer.Serialize(DateTime.Now)

produces:

 new Date(Date.UTC(2006,9,27,8,59,53,506))

Did you implement that in a later version? Or only in the 2.0 libraries?

# re: AjaxPro JSON Parser and Dates

Friday, October 27, 2006 11:31 AM by Michael Rumpler

I try again 'cause my first comment still does not appear after 6.5 hours.

I am using version 6.10.6.2 of the AjaxPro.dll and AjaxPro.JSON.dll but the result of

  AjaxPro.JavaScriptSerializer.Serialize(DateTime.Now)

is

  new Date(Date.UTC(2006,9,27,8,59,53,506))

and not the string that you wrote.

What I would need is the other direction, but this doesn't work either:

  AjaxPro.JavaScriptDeserializer.DeserializeFromJson("2006-10-27 10:00:00", typeof(DateTime))

throws an

  System.NotSupportedException: Specified method is not supported. at AjaxPro.DateTimeConverter.Deserialize(IJavaScriptObject o, Type t)

Michael