Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-like pickling of full Javascript objects

Tags:

Is there any serialization framework for Javascript which would retain class and reference information like Python pickles? I.e. one could directly take a prototypial inherited class instance (not just JSON-like data) and serialize it like::

 // Somehow add serialization metadata to classes first  obj = new MyObject();  obj.referred = new MyObject2();  pickle = serializer.dump(obj) // Provides byte stream of the serialized object   

The serializer could take care of

  • Encoding class information in the pickle - this should be somehow customizable due to different JS class systems in existence

  • Automatically follow and serialize referred objects

  • Provide hooks to add custom encoders/decoders for values (dates being the most common case)

This would making internal storing and transferring of complex data structures little bit easier. I am hoping to use this in a game engine. Like with pickles, the deserialization of data would not be possible without the orignal Javascript code providing the class definitions.

What kind of such frameworks exist for Javascript already exist or I am going to need to roll-out a custom system?

like image 675
Mikko Ohtamaa Avatar asked Mar 04 '13 16:03

Mikko Ohtamaa


People also ask

Does Python have objects like JavaScript?

JavaScript objects are like Python classes (because they inherit like Python classes). For JavaScript attribute and item access are the same. This is achieved in Python by providing custom item methods. In Python the custom item methods must be placed on the type of the object (or a superclass of its type).

What can I use instead of a pickle in Python?

An alternative is cPickle. It is nearly identical to pickle , but written in C, which makes it up to 1000 times faster. For small files, however, you won't notice the difference in speed. Both produce the same data streams, which means that Pickle and cPickle can use the same files.

Can you pickle objects Python?

Python comes with a built-in package, known as pickle , that can be used to perform pickling and unpickling operations. Pickling and unpickling in Python is the process that is used to describe the conversion of objects into byte streams and vice versa - serialization and deserialization, using Python's pickle module.

What is a pickling in Python?

“Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy.


1 Answers

It doesn't fit perfectly but you can try to use occamsrazor.js . Doing this you can use JSON serialization:

// this is your costructor function function Circle(attrs){     this.radius = attrs.radius; } Circle.prototype.area = function (){     return this.radius*this.radius*Math.PI; } Circle.prototype.perimeter = function (){     return 2*this.radius*Math.PI; }  // this is a validator function hasRadius(obj){     return radius in obj; }  // this is your factory function objFactory = occamsrazor().addContructor(hasRadius, Circle);  // this is your deserialized object var json = '{"radius": 10}';  // circle now is a full fledged object var circle = objFactory(JSON.parse(json)); 

The drawback is that you don't get a snapshot of an object like using pickle, you recreate a new object. But it may be convenient in some circunstances.

like image 162
sithmel Avatar answered Sep 21 '22 12:09

sithmel