Working With DateTime JSON Serialization in, July 27, 2012

Like many other software developers who work with, I have had the misfortune of having to work with the non-standard way that the .net JavaScriptSerializer class serializes DateTime objects into JSON.

The problem is that the ES3 ECMAScript specification does not specify a DateTime literal for serialization. Microsoft made a judgement call and created their own JSON serialization for DateTime objects. The ES5 ECMAScript specification does specify how to serialize DateTime objects (ISO 8601 specification), but many browsers don't yet support this. The JavaScriptSerializer class serializes a DateTime object as a string of the following format: "\/Date(T)\/" where T is the number of milliseconds since January 1, 1970 at midnight, UTC. (MSDN Reference)

The rationale for this particular format is that a forward slash "/" would not normally need to be escaped, so an escaped forward slash "\/" stands out as something out of the ordinary. Of course, once this string is deserialized, it is stored in memory as the string "/Date(T)/".

What I find extremely frustrating about this format is the fact that even if my application is entirely in .net (no other frameworks involved, such as Java or Python), I cannot pass the result of deserializing this JSON string to another .net web service. For instance, if I have one web service call that returns a DateTime object in this manner, I cannot simply pass that string in to another web serve and expect it to be deserialized; an exception will be raised.

The above information is available from many sources. What I have to offer here is my personal solution to the problem.

Assuming that we use a framework, such as jQuery, to deserialize this JSON, we will be left with the string "/Date(T)/". The simplest way I know of to convert this string literal to a JavaScript Date object is new Date(parseInt(("/Date(T)/").substring(6))). This will find the digits at index six and pass them to the Date constructor, as in the example to the left.

The above example shows how to get the DateTime object represented by a single string returned by the JavaScriptSerializer class. I can't say that I figured this out myself. I found this solution on StackOverflow. A more difficult problem I ran into was when I wrote a web service that returned a complex object containing many different DateTime objects, all serialized to JSON. I would like to share my solution to this problem. My solution is not perfect, but it works well for the problem I faced.

My solution was to write a function that recurses over the members of an object passed in, looking for strings that match a regular expression for "/Date(T)/". As you look over the code, keep in mind the fact that in JavaScript, strings are always passed by value and objects are always passed by reference.

Unfortunately, this still leaves me with the task of invoking the function that I've written on every object I get from a .net web service call that could possible contain these odd date strings. However, this function does give me date objects that I can pass to another web service call.

The bottom line, though, is that this is a temporary workaround until there is widespread adoption of the ES5 standard. Once that happens, your grandchildren may abandon this sort of workaround. In the meantime, remember that these literals contain escaped slashes "\/" that ordinarily do not appear in JSON strings. Since I use jQuery to get these DateTime strings from the .net web services, it might make sense to write an extention to jQuery to search for these.