ASP.NET AJAX JSON Date serialization

The Beta 1 Editon of Active Server Pages DOT NET Asynchronous Java Script And XML Java Script Object Notation (ASP.NET AJAX JSON) have changed the serialization of variables type data. The Christmas date 2000 will be included in a JSON String like

{"name":"Hannes","gebdat":"@977612400000@"}

I have no idea why and also no idea how to get the value as Jscript date object.

To get the JScript object from JSON String is easy by default.

var o;

o=eval('('+JSONSTRING+')');

Then the propertys can be accessed like

alert(o.name);

My workaround for extracting the date

var tmp =o.gebdat.split('@');

var heute= new Date(parseInt(tmp[1]));

alert(heute);

I have doubts that this is the best way. There must be some idea behind the @ format.

Published Sunday, October 29, 2006 12:55 PM by preishuber
Filed under: , ,

Comments

# re: ASP.NET AJAX JSON Date serialization

Sunday, October 29, 2006 7:33 AM by Ron Buckton

If you are using ASP.NET AJAX the best way to deserialize the JSON is to use Sys.Serialization.JavaScriptSerializer.deserialize(jsonString)

It handles the date parsing for you.

# re: ASP.NET AJAX JSON Date serialization

Sunday, October 29, 2006 8:34 AM by preishuber

after the hint from Ron Buckton i took a look a the underl-ying script library. Microsoft is using regular expression to replace the @

var exp = data.replace(new RegExp('\\"@(-?[0-9]+)@\\"', 'g'), "new Date($1)");

this is a quite expensive method. My own inplementation is 3 times faster.

# re: ASP.NET AJAX JSON Date serialization

Sunday, October 29, 2006 9:55 AM by Garbin

Hi,

it seems that the @ is used as a convention to indicate a "date literal". More info on Nikhil Kothari's blog: http://www.nikhilk.net/DateSyntaxForJSON.aspx

# re: ASP.NET AJAX JSON Date serialization

Sunday, October 29, 2006 10:08 PM by Marc Brooks

Garbin,

Yes, that's why it's done, a suggestion from Nikhil... but why not do it the way nearly everyone else does and emit a "new Date(xxx)" constructor directly?  It's tons clearer and basically as fast as possible...

# re: ASP.NET AJAX JSON Date serialization

Thursday, November 16, 2006 6:27 AM by Nikhil Kothari

Also look at http://www.nikhilk.net/DateSyntaxForJSON2.aspx for the why this way as opposed to new Date().

Note from my comment response there: Regex is not too bad, unless your responses are just unreasonably huge. Furthermore, if you reuse the same regex instance, usually the script engine will optimize it. Breaking apart a string, and re-constructing it, esp. if you have several date values embedded in the JSON isn't that performant either.

Secondly, don't use eval directly. Use Sys.Serialization.JavaScriptSerializer.deserialize, so you don't have to do this date conversion manually.

# re: ASP.NET AJAX JSON Date serialization

Saturday, December 16, 2006 5:46 PM by Jan

Folks, please do not ignore time zones. I posted an article on this recently: http://restingbird.info/articles/2006/12/15/date-serialization-in-json

# re: ASP.NET AJAX JSON Date serialization

Friday, May 16, 2008 1:43 AM by Hunka

In short, its very helpful. Thanks :)

# re: ASP.NET AJAX JSON Date serialization

Monday, June 01, 2009 2:58 PM by Brett Nieland

Thanks!

Leave a Comment

(required) 
(required) 
(optional)
(required)