Sunday, March 11, 2012

DateTime Serialization Bug for ScriptManagers GetScriptDescriptors method.

Upon further investigation, I narrow the problem down to the System.Web.Script.Serialization.JavaScriptSerializer class.

So basically if I do this in C#

JavaScriptSerializer serializer = new JavaScriptSerializer();

Console.WriteLine(serializer.Serialize(DateTime.Now));

the resulting output will be: "\/Date(1177439632904)\/"

*with double quote.

and I think it should be:

Date(1177439632904)

This got to be intentional, anybody knows why??


This date format has a very specific purpose... if you're interested in the history behind it, definitely read my explaination in this post:

http://forums.asp.net/thread/1558671.aspx

However, you shouldn't ever have to deal with this date format directly, it happens behind the scenes and automatically. The fact your property is ending up as a string on the client is a problem we're looking into.

I assume that you have a script component (either a IScriptControl or ExtenderControl) with a client-side type, correct? In that case, you could work around this problem by providing a string property on the client side type to hold the date. Keep the real property as a date. Have the ScriptDescriptor add the date to the string based property. Then in the string based property on the client, check the value being set -- if its a string, deserialize it using the javascript deserializer and set it to the real property -- otherwise just set it directly on the real property. This scheme would allow your component to work as if the date were output correctly, and would continue to work if it ever changed, so you won't be broken.


Thanks for your reply. It seems that the reason that it is done this way is to avoid deserization of a string that look like "Date(124)" into a DateTime object.

But isn't string always wrapped by double quotes, where a Date object shouldn't be wrapped.

So in the JSON like bellow

{ string: "Date(123)", date: Date(123) }

You should be able to tell them apart..

am i missing something?

Rushui


"{ date: Date(123) }" unfortunately is not valid JSON.

http://www.json.org/

Our serializer makes every effort to only produce valid json. JSON itself doesn't have a date literal format.

No comments:

Post a Comment