serialization - How to serialize & deserialize Javascript objects? -


i need serialize , deserialize javascript objects store them in db.

note these objects contain functions, can't store them json, can't use json2.js.

what's state of art in [de]serialization of javascript objects (in javascript of course).

thanks, stewart

in general, there's no way (in browser) serialize objects functions attached them, since every function has reference it's outer scope. if function references of variables, won't exist anymore when deserialize it.

what use built-in (or json2.js) json.stringify , json.parse functions replacer , reviver parameters. here's partial example of how work:

json.stringify(yourobject, function(name, value) {     if (value instanceof latlng) { // check name if want         return 'latlng(' + value.lat() + ',' + value.lng() + ')';     }     else if (...) {         // other type needs custom serialization     }     else {         return value;     } });  json.parse(jsonstring, function(name, value) {     if (/^latlng\(/.test(value)) { // checking name safer         var match = /latlng\(([^,]+),([^,]+)\)/.exec(value);         return new latlng(match[1], match[2]);     }     else if (...) {         ...     }     else {         return value;     } }); 

you can use serialization format want in custom types. "latlng(latitude,longitude)" format 1 way of doing it. return javascript object can serialized json natively.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -