If you try and store and retrieve a JSON object in a cookie using the
cookie plugin (http://plugins.jquery.com/project/cookie), you get an
unexpected result.

For instance, save into cookie:
$.cookie("mydata", {foo:"bar",baz:1});

Retrieve value:
var mydata = $.cookie("mydata");

But that returns [object Object]


A json parser (e.g. http://www.json.org/js.html) has to be used.

Using this script, you can 'stringify' the object before storing it:
$.cookie("mydata", JSON.stringify({foo:"bar",baz:1}));

Then you retrieve it and use JSON.parse:
var mydata = JSON.parse($.cookie("mydata"));


While this is a workaround, perhaps it may be a good idea if the
cookie plugin did this automatically (i.e. convert object to string
and back again)?

Rather than building in JSON parsing, maybe make the json.org parser a
prerequisite?

Then a check is done in the plugin code:

if(typeof JSON != "undefined")
{
        if(typeof value == "object")
                value = JSON.stringify(value);
}

Reply via email to