Although cookies almost seem like a prehistoric concept in web development, they’re a well understood commodity that still serve useful purposes. As such, I wanted to write up a quick column that demonstrates a common pattern involving cookies and JSON that you may find useful from time to time.
Dojo’s cookie module provides a consistent interface for getting and setting cookies. The simple dojo.cookie function handles the messy details of parsing the cookie string and is dual purposed: it acts as a getter if passed only one argument but acts as a setter if passed a second argument. In the case of acting as a setter, an additional third parameter allows you to pass in properties such as when the cookie expires, how to handle the cookie if a secure connection is not present, etc.
For example, you might set a key/value pair of foo=23 to expire after 5 days via a call to dojo.cookie("foo", "23", {expires : 5}). You could fetch back the value for foo via dojo.cookie("foo") but you’d have to also remember to use parseInt to cast it to a Number since cookies are returned as String values.
Well, managing lots of key/value pairs in this way gets cumbersome pretty quickly, so let’s use the dojo.toJson and dojo.fromJson functions to manage the serialization of more advanced data structures without much more effort. Thus, here’s that pattern I was referring to:
dojo.require("dojo.cookie"); //Pull down the dojo.cookie module
/* Serialize a complex data type... */
var foo = {bar : 0, baz : 23, quz : ['a','b','c']};
dojo.cookie("foo", dojo.toJson(foo)); //by default, the cookie will expire at the end of the session
/* ...and then deserialize it when you need it back. */
foo = dojo.fromJson(dojo.cookie("foo")); //no further typecasting required
So, in other words, the next time you find yourself manipulating more than a few cookies, you may as well wrap them up into a hash and gain the benefits of JSON serialization, which provides the additional nicety of automatic typecasting.
My upcoming book, Dojo: The Definitive Guide includes coverage of simple patterns like these and a whole lot more. (See my last post for a table of contents preview.)


Matthew,
Your book looks really good.
Is there any change O'Reilly will make it available as an eBook, ie, PDF?
Neville
Neville - As far as I know, it will be available as a PDF sometime very soon after the print version is available, which is on or before 17 June at this point. For what it's worth, I find myself using Spotlight to quickly lookup info in the latest draft version that I have here locally already.
Hey,
Any update on the date of the release of your book...I'm waiting very patiently!
Will it still be available before June 17th?
Thanks!
Hey Rachel - The *final* edits have taken place and all that's really left now is the actual printing. AFAIK, 17 June is a "no later than" date. I'm actually hopeful to get a box of these books in the mail early next week though I haven't received any info that would confirm this. I'll dig around and see what I can find out and post back here if I discover anything. BTW, thanks for your interest in the book. I really hope you'll find it helpful.
Neville - Hey, I just discovered that the current policy that O'Reilly has is to make a book available as a PDF 60 days after it has been available in paper. I know, I know...not the most desirable circumstances imaginable, but the good news is that they're considering a change to the policy. My advice would be to drop them a line using the "Contact Us" link at the bottom of the page and express interest in getting the PDF available sooner. Who knows. You might be the tipping point.
Sounds good, I'll pre-order now!