Wishful Thinking - Date Literal Syntax for JSON

Why doesn't JavaScript include a literal syntax for Date objects? This results in a wrinkle in the otherwise simple and elegant JSON-based serialization used in AJAX apps. Heres a proposal...

The simplicity of JSON or JavaScript Object Notation has established it as the preferred object serialization format used in AJAX apps, despite the fact that "X" is supposed to stand for XML. So as you'd expect, in Atlas, we use it to send and receive data to and from the server when. Essentially JSON is based on literal syntax for primitive types, arrays and objects, allowing you to specify atomic values, group them in arrays or records which can be further grouped recursively, thereby allowing you to define an arbitrary object graph (as shown visually and explained in further depth here).

However there is one wrinkle to this model. There is no literal syntax for Date objects, like there is for strings, booleans, and numbers ("abc", true, and 123). Even Regex objects can be expressed as literals such as /d{2}$/. So its not clear why the date type was ignored in the original JavaScript language design. Despite being fairly common, you are required to create dates by calling new Date(…). It seems like a big oversight. I'd be curious if there was in fact some conscious thought behind this omission.

Consequently, in our JSON serialization in Atlas we do use the Date ctor to serialize date instances, both on the server and on the client. Besides being an exception to the rule, this requires the server-side JSON parser (which can't simply eval the serialized string format to implement deserialization) to have to understand new ctor expressions, which are an inherently more flexible language construct and consequently a tad bit more complex to deal with than literal values.

This has bugged me since we first started work on Atlas. I'll go ahead and propose a syntax that’s been on my mind for just about as long: @04/15/2006@, or @04/15/2006 12:00:00 PM@. Basically I am proposing the use of "@" which reads out as "at" as a logical choice to represent date instances. The content within can be anything that is already supported by Date.parse().

Maybe something along these lines can be considered for a future version of the JavaScript/EcmaScript syntax, but then theres the question of broad availability across script engine implementations before it can be depended upon. This is where the roughly 5 year maturity period required for any new technology on the Web to be usable kicks in… bummer!


[ Tags: | | | ]
Posted on Sunday, 4/16/2006 @ 1:27 AM | #Ajax


Comments

16 comments have been posted.

RichB

Posted on 4/16/2006 @ 1:57 AM
Why not use # to delineate the date literal like VB?

Paolo Marcucci

Posted on 4/16/2006 @ 8:04 AM
Before committing to ANYTHING, remember the time zone :)

Matt

Posted on 4/16/2006 @ 9:00 AM
# or @ or ? -- I don't think it matters. The bottom line is that in order to expedite parsing, some consistent deliniation is necessary. The nice thing about the @ is that I can't think of any languages that use it as a beginning comment character like the ' for vb or // for c derivatives or the # for ruby.

Nikhil Kothari

Posted on 4/16/2006 @ 9:32 AM
Like Matt says, I am not so hung up on a particular character... rather the proposal is more about the overall idea. While I was at it I thought @ fitted the context best. :-)

Anything that Date.parse() supported would be supported, so that includes time zone information.

Steve Maine

Posted on 4/16/2006 @ 9:48 AM
Can Date.parse handle ISO 8601 dates?

Nikhil Kothari

Posted on 4/16/2006 @ 10:05 AM
The spec at http://www.ecma-international.org/publications/standards/Ecma-262.htm doesn't indicate the exact specifications of the date format allowed. All it says (on pg 122) is the date may be a local time, UTC time, or time in some other time zone... which leads me to think its a fairly flexible/loose format, so that what is allowed is based on some original implementation of JavaScript. Maybe someone else has pointers to more detailed spec around Date.parse?

Anthony Mills

Posted on 4/16/2006 @ 10:49 AM
If you're proposing a standard date format, don't choose something that's subject to the month/day oddities or time zone changes. And don't be as flexible as Date.parse(); be rigid! After all, that way, server-side implementors have a chance of getting it right.

Just choose a fairly small subset of ISO 8601 and you should be OK.

Andrey Skvortsov

Posted on 4/20/2006 @ 10:43 AM
Slightly off topic;-)How properly I can implement/extend "auto" complete behavior to support text/value pair aka <select/>-not only text part.It would be useful also to have ability to control of sync text/value-text is value binded,if text is changed it has to be seen only as template for query-not real "text".Text only selected if value is selected and rollback to previous "text",if no item selected directly in "combo".

Thanks.

Rick Strahl

Posted on 4/25/2006 @ 12:45 AM
.NET doesn't have a date literal either <s>, but then at least Date.Parse is a bit more flexible in what you can pass to it. In my JSON implementations I use UTC/GMT date parsing for sending from the client to the server and visa versa and that seems to work well. If I recall correctly it takes custom parsing for .NET to properly read the JavaScript data string. I agree though - I wish there was a cleaner way to do this.

I posted something to that effect here:
http://west-wind.com/weblog/posts/5054.aspx

Nikhil Kothari

Posted on 4/28/2006 @ 11:34 AM
Just a quick update - I've sent a pointer to this post to the Ecma/JavaScript folks, and I believe this is being considered for being addressed.

Saurabh

Posted on 5/1/2006 @ 7:47 PM
There is a lot to learn from you, u re quite a resource Nikhil, i wanna be like you, simply excellent. will you be my mentor? please?

Thanks,
Saurabh

www.paketim.com

Posted on 5/5/2006 @ 2:44 AM
Great post, Thanks.

David K

Posted on 5/15/2006 @ 1:34 PM
Great post. I'm just getting started with JSON and I quickly came across this limitation. You mention in your blog that you overcame this in Atlas using the Date ctor. Would you elaborate on this technique please? I'm trying to serialize an object that has a Date as a property, but it comes out as "myDate" : {}

Cheers

Nikhil Kothari

Posted on 5/15/2006 @ 6:07 PM
What we do in Atlas is special case date objects. So if I had say an "Task" object that had say a startDate field, the JSON for that would be:
{ startDate: new Date(...) }
We special case it both on serialization and deserialization ends.

I believe JavaScript 2 will indeed have date literals. Again, the special case/workarounds will unfortunately be around for some time.

David K

Posted on 5/15/2006 @ 10:22 PM
Thanks. I think I understand. So did you change toJSONString() to check for objects of type Date and then serialize them as you've described? Or do you create your own Date class that knows how to serialize? Or is there another option?

Nikhil Kothari

Posted on 5/16/2006 @ 8:17 AM
The convention in Atlas is to allow a type to participate in serialization by providing a serialize method on the type. So if you look at the Atlas scripts, you'll see we send down Date.serialize = function() { ... }

The JSON serializer looks for this method, and calls it if it finds it.
The discussion on this post has been closed. Please use my contact form to provide comments.