Javascript Serialization Of Typed Objects
Solution 1:
For a start, here is a simple example of custom serialization / deserialization:
function Person( name, sex, age ) {
this.name = name;
this.sex = sex;
this.age = age;
}
Person.prototype.serialize = function () {
var obj = this;
return '{ ' + Object.getOwnPropertyNames( this ).map( function ( key ) {
var value = obj[key];
if ( typeof value === 'string' ) { value = '"' + value + '"'; }
return '"' + key + '": ' + value;
}).join( ', ' ) + ' }';
};
Person.deserialize = function ( input ) {
var obj = JSON.parse( input );
return new Person( obj.name, obj.sex, obj.age );
};
Usage:
First, we create a new instance object:
var person = new Person( 'John', 'male', 25 );
Now, we serialize that object into a string using the Person.prototype.serialize
method:
var string = person.serialize();
This will give use this string:
{ "name": "John", "sex": "male", "age": 25 }
Finally, we deserialize that string using the Person.deserialize
static method:
var person2 = Person.deserialize( string );
Now, person2
is an instance of Person
and contains the same property values as the original person
instance.
Live demo: http://jsfiddle.net/VMqQN/
Now, while the Person.deserialize
static method is required in any case (it uses JSON.parse
internally, and invokes the Person
constructor to initialize a new instance), the Person.prototype.serialize
method on the other hand, is only needed if the built-in JSON.stringify
static method doesn't suffice.
In my example above var string = JSON.stringify( person )
would get the job done too, so a custom serialization mechanism is not needed. See here: http://jsfiddle.net/VMqQN/1/ However, your case is more complex, so you'll need to define a custom serialization function.
Solution 2:
If you look at the ST-JS (http://st-js.org) project, it allows to create your object graph in Java on the server side, serialize it in JSON, and deserialize it on the client side (Javascript) in a typed manner, i.e. the objects will be instantiated using their constructor and you can call methods on the created objects.
To use ST-JS you should write your client code is Java, that is converted almost one-to-one in Javascript.
The AJAX/JSON chapter on the home page of the site explains you how to parse a JSON string while keeping the type information.
Solution 3:
My question is, how is the resulting object supposed to get deserialized as a MapLayer object rather than as a generic Object. And how are all the Sprite instances supposed to get deserialized as sprites.
I've made an npm module named esserializer to solve this problem: save JavaScript class instance values during serialization, in plain JSON format, together with its class name information:
const ESSerializer = require('esserializer');
const serializedText = ESSerializer.serialize(anInstanceOfClassMapLayer);
Later on, during the deserialization stage (possibly on another machine), esserializer can recursively deserialize object instance, with all Class/Property/Method information retained, using the same class definition:
const deserializedObj = ESSerializer.deserialize(serializedText, [MapLayer, Sprite]);
// deserializedObj is a perfect copy of anInstanceOfClassMapLayer
Should I be using "new MapLayer()" instead of "{}"? Or am I simply supposed to include the prototype and constructor properties of the object in the serialization?
Inside ESSerializer, class name information is included during serialization, and all prototype properties are re-constructed during deserialization.
I don't want to serialize the tileset as an object that gets constructed as a new object during de-serialization, but rather as a reference to an existing object.
Unfortunately, it is impossible. The serialization happens on one machine and the deserialization may happen on another computer -- anyway, this is what serialization/deserialization supposed to do. Computer would never know "an existing object" on another machine.
Post a Comment for "Javascript Serialization Of Typed Objects"