Dates and JSON
JSON is so very handy but one thing I hate about JSON is that its spec is so lightweight that it sometimes lacks some details on specific areas such as for dates. Scott Hanselman’s post “On the nightmare that is JSON Dates. Plus, JSON.NET and ASP.NET Web API”, is actually a perfect example of the kind of troubles you run into when building web sites and using various JSON serializers.
We bumped into this as we’re working on the next version of our ASP.NET Web Producer which will ship with an Ajax web site template (JQuery + JqGrid + JSON/REST) allowing developers to generate business oriented web apps (see our roadmap post for more info on this subject).
Anyway, when developing a business web site we, developers, have to support dates and since their format can vary in JSON, our JSON serializer/deserializer class (named JsonUtilities) supports several:
- \/Date(1330848000000-0800)\/, with or without “TZ“ by the way (this is our default, read this to know why: http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx)
- new Date(utc milliseconds)
- ISO 8601
So if ever you need to support several date formats without having to switch JSON serializers/deserializers for each scenario, keep this JsonUtilities class in a corner as I’m sure it’ll come handy.
By the way, this JsonUtilities class is shipped as part of a free NuGet package, independently from our product, named CodeFluentRuntimeClient.
Here’s it’s web page if you want to try it out (http://nuget.org/packages/CodeFluentRuntimeClient) and here’s a post containing some serialization examples (JsonUtilities – Part 2)