Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize & deserialize JavaScript objects?

I need to serialize and deserialize JavaScript objects to store them in a DB.

Note that these objects contain functions, so I can't store them as JSON, so I can't use json2.js.

What's the state of the art in [de]serialization of JavaScript objects (in JavaScript of course).

like image 874
Stewart Johnson Avatar asked Aug 31 '10 11:08

Stewart Johnson


People also ask

What is a serialize method?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

How do you serialize a method in Java?

Since Method does not implement Serializable, it cannot be serialized using the standard Java Serialization API. A workaround would be to manually serialize just the name of the class and method and its parameter types. You can then recreate the Method instance during deserialization.

What it means to serialize?

Serialization is the process of converting a data object—a combination of code and data represented within a region of data storage—into a series of bytes that saves the state of the object in an easily transmittable form.


2 Answers

In general, there's no way (in a browser) to serialize objects with functions attached to them: Every function has a reference to its outer scope, that scope won't exist when you deserialize it, so serialized references to that scope will be invalid.

What I would do is use the built-in (or json2.js) JSON.stringify and JSON.parse functions with the replacer and reviver parameters. Here's a partial example of how it would work:

JSON.stringify(yourObject, function(name, value) {     if (value instanceof LatLng) { // Could also check the name if you want         return 'LatLng(' + value.lat() + ',' + value.lng() + ')';     }     else if (...) {         // Some other type that needs custom serialization     }     else {         return value;     } });  JSON.parse(jsonString, function(name, value) {     if (/^LatLng\(/.test(value)) { // Checking the name would be safer         var match = /LatLng\(([^,]+),([^,]+)\)/.exec(value);         return new LatLng(match[1], match[2]);     }     else if (...) {         ...     }     else {         return value;     } }); 

You can use any serialization format you want in your custom types. The "LatLng(latitude,longitude)" format is just one way of doing it. You could even return a JavaScript object that can be serialized to JSON natively.

like image 150
Matthew Crumley Avatar answered Sep 20 '22 09:09

Matthew Crumley


You don't want to serialize logic such as functions.

If you have to update your logic / js functions in the future, you don't (always) want the older logic to be loaded back with the data neccessarily. Beware.

like image 36
lknox Avatar answered Sep 20 '22 09:09

lknox