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?
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).
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.
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.
“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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With