Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

serialize/unserialize in jQuery [closed]

Is there something like serialize/unserialize PHP functions in jQuery?

These functions return a string representations of an array or an object which can than be decoded back into array/object.

http://sk2.php.net/serialize

like image 884
Richard Knop Avatar asked Dec 09 '22 19:12

Richard Knop


1 Answers

jQuery's serialize/serializeArray only works for form elements. I think you're looking for something more generic like this:

http://code.google.com/p/jquery-json/

This plugin makes it simple to convert to and from JSON:

var thing = {plugin: 'jquery-json', version: 2.2};

var encoded = $.toJSON(thing);        
//'{"plugin":"jquery-json","version":2.2}'
var name = $.evalJSON(encoded).plugin;
//"jquery-json" 
var version = $.evalJSON(encoded).version;
// 2.2

Most people asked me why I would want to do such a thing, which boggles my mind. Javascript makes it relatively easy to convert from JSON, thanks to eval(), but converting to JSON is supposedly an edge requirement.

This plugin exposes four new functions onto the $, or jQuery object:

  • toJSON: Serializes a javascript object, number, string, or arry into JSON.
  • evalJSON: Converts from JSON to Javascript, quickly, and is trivial.
  • secureEvalJSON: Converts from JSON to Javascript, but does so while checking to see if the source is actually JSON, and not with other Javascript statements thrown in.
  • quoteString: Places quotes around a string, and inteligently escapes any quote, backslash, or control characters.
like image 182
meder omuraliev Avatar answered Dec 13 '22 21:12

meder omuraliev