Wednesday, July 07, 2010

An Exception with AjaxPro Deserialization of JSON DateTime value that comes from ExtJS DateField input control

ExtJS DateField control has JSON object in its value property. Once being sent to a server it looks like:
Attempt of parsing it using JavaScriptDeserializer<T> from AjaxPro library leads to NotSupportedException with "word /" message. Looking through AjaxPro source code I found that this happens because AjaxPro expects JSON Date string to be in slightly different format, i. e.:

ExtJS returns: /Date(1278425211248)/
AjaxPro consumes: "\/Date(1278425211248)/"

There is a simple workaround here:
string s = "/Date(1278425211248)/";
var json = String.Concat("\"\\", s, "\"");
DateTime dt = JavaScriptDeserializer.DeserializeFromJson<DateTime>(json);
Yes, the simplest way is just to surround a JSON Date string with required characters.
It is funny that Scott Hanselman faced with this problem back in 2006, but he was just playing with different Ajax libraries and just had rid of DateTime field to make it work :)

No comments: