© 2019 Meouzer Consortium

JavaScript: Serialization/Deserialization of Classes

loading
Finding a serialization algorithm for JavaScript is tough. Finding one while riding a Bull is tougher. Finding one while riding a Bull jumping out of an airplane is tougher than tough. Finding one while riding a Bull jumping out of an airplane while eating a Butter Finger in all its crispity crunchity peanut buttery glory is tougher than tougher than tough. Taking the Butter Finger away from the Bull is just plain stupid.
Meouzer

Introduction

Open the console window to see that all tests of Test-Ser-Des.js have been passed. If you are using IE11, then you must comment out all code attempting to change constants because such are fatal errors at compile time. Or just hook up the Test-Ser-Des-IE11.js file.

The How to Do It Section

The Serialization and Deserialization Functions of the foo Class
function foo(x,y) { // variables. functions that aren't methods are variables. var a = ...; const b = ...; let c = ...; (function nestedFunction(x,y,z) { // No class methods defined inside nestedFunction. // Changes to variables of the class allowed. // Well it's actually OK if the method is // context equivalent to a method written // outside. E.g., a method here is ok as // long as it doesn't depend on the local x,y,z. })(5,7); this.serialize = function() { var cleanContextObject = {this$:this, a:a, b:b, c:c, x:x, y:y}; return serializeClassData(cleanContextObject); } } // write deserialization function immediately after constructor // so both have the same outer context. foo.deserialize(str) { return (function () { this.evaluator = function(){return eval('('+ arguments[0] +')');}; this.contextObject = parseDataString(str, this.evaluator, Object.create(foo.prototype), "foo"); var a = this.contextObject.a; const b = this.contextObject.b; let c = this.contextObject.c; var x = this.contextObject.x; var y = this.contextObject.y; return this.contextObject.this$ }).call({}); }