Skip to content

Saving JSON object as value fails. #89

Closed
fredsterss opened this Issue Jul 31, 2012 · 12 comments

4 participants

@fredsterss

ie. $.cookie('some_key': JsonObj) returns null

This is because the object is not automatically stringified. Perhaps this would be useful? Ie. if a regular object is detected, stringify and then parse when the cookie is returned.

@carhartl
Owner
carhartl commented Aug 1, 2012

Let's say if an object is detected and JSON.stringify is defined.

@carhartl
Owner
carhartl commented Aug 1, 2012

Though we can then just always stringify instead of String(value) I guess.

@carhartl
Owner
carhartl commented Aug 1, 2012

Thinking some more about this I am sure that when automatically stringifying input, users would also expect to have the value parsed back to JSON automatically when reading the cookie. Which IMHO is out of scope of the plugin. Allbeit on a smaller scale, we're not doing this for numbers either:

$.cookie('number', 1);
$.cookie('number'); // => '1'
@carhartl
Owner
carhartl commented Aug 1, 2012

Though an option to provide encode/decode functions were probably feasible.

@FagnerMartinsBrack
Collaborator

Not every browser supports window.JSON, so you should create a polyfill to handle that. It would increase drastically the size of the plugin.
https://github.com/douglascrockford/JSON-js/
http://code.google.com/p/jquery-json/

My suggestion is to stringify/parse the values before and after the cookie creation (that is the way I do) or create another plugin to handle that automatically.

Something like (not tested):

    $.jsonCookie = function(key, val, options) {
       if( val ) {
          $.cookie( key, $.toJSON? $.toJSON(val) : val, options );
       } else {
          return $.parseJSON ? $.parseJSON( $.cookie( key, val, options ) ) : $.cookie( key, val, options );
       }
    };

Or an option to provide encode/decode as mentioned above...

@fredsterss

Ok - even if it's not included then an error message would at least be useful, ie. 'this data was not saved because it is in object form' or some such.

@FagnerMartinsBrack
Collaborator

To make a consistent check we should use regex, increasing the plugin size without any reason (at least at my point of view). I believe a lightweight cookie plugin should just transfer data using string, but is up to the author decide if this is actually needed (I don't remember anybody else asking for this since I started watching this repo).

@carhartl
Owner
carhartl commented Aug 2, 2012

Probably should simply be documented that one has to pass a string as value. Because I too believe that validating arguments is not the plugin's responsibility, at least when it should be as lightweight as possible. I think providing hooks for read/write is giving best of both (won't add too much code and gives developer flexibility).

@fredsterss

OK. My only point is it currently is not obvious why it fails, and I'm not sure it follows the pattern that jQuery has established of basically doing everything to make the developer's life easier. Surprising that noone before me has tried to put an object into a cookie and been confounded by why it didn't work. Maybe I'm just a n00b ;)

@FagnerMartinsBrack
Collaborator

You are not the only one, I tried to pass a JSON the first time also, but failing made me realize that the plugin's behavior was the correct in my point of view ;D.
Well... jQuery has an internal JSON parse ($.parseJSON) which checks for JSON.parse existence but I could'nt find anything about a stringify or $.toJSON (I am not familiarized so much with jquery core).
But even if it exists we would be using a non documented API, which always has the risk to break some day.

@jpillora

Just opened a pull request for this #93 (Updated)

@carhartl
Owner

See discussion here: #93

-> Solution could be a json option that needs to be set as a default.

@carhartl carhartl closed this in 796abd6 Aug 29, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.