Serializes objects into a string, each object is passed with a label associated with it. Objects are passed in an object literal {label1: object1, label2: object2, ..., labelN: objectN}.

setSerializationIndentation(indentation)

Set the indentation level of the serialization string (in number of spaces).

Deserializer(serializationString)

constructor

Creates a deserialization object to deserialize the objects serialized in serializationString.

deserializeObject()

Returns a promise for an object that was serialized with serializeObject.

deserialize(instances)

Returns a promise for the objects that were serialized with serialize. This result is an object literal with the deserialized objects and their respective labels: {label1: object1, label2: object2, ..., labelN: objectN}.

The instances parameter allows to override the deserialization of specific objects by using the instance passed instead, they are passed in an object literal: {label1: object1, label2: object2, ..., labelN: objectN}.

The logic to serialize a custom object is handled by the Visitor object. This is the object that visits the graph of objects traversed during serialization and knows what data to store for each type.
The interface expects a getTypeOf function that returns the type of the object and a visit<Type> function that knows what data to store from the object. Multiple visit<Type> can be defined as long as getTypeOf returns different types.

The logic to deserialize a custom object is handled by the Reviver object. This is the object that knows how to revive objects. The interface expects a getTypeOf function that returns the type of the object and a revive<Type> function that knows how to revive the object. Multiple revive<Type> can be defined as long as getTypeOf returns different types.

Example to deserialize:

{

"root":{

"type":"map",

"object":{/* map data */}

}

}

Reviver.addCustomObjectReviver({

getTypeOf:function(value){

if(value.type==="map"){

return"Map";

}

},

reviveMap:function(value,context,label){

var map =newMap(value.object);

if(label){

context.setObjectLabel(map, label);

}

return map;

}

});

The result of the deserialization is:

{

root:<MapObject>

}

Reviver functions can be asynchronous by returning a promise to the revived value.

The context object is given as the second parameter to all revive* functions and it is used to set labels on deserialized objects, so they can be accessed after the deserialization, and to get objects that were serialized under a specific label.

During serialization an AST-like object is created that holds all the data needed to be serialized. It is the role of the Visitor to create this AST.

When the AST is finished it is the role of the Builder to generate an output format by reading the AST. The Builder presented in Mousse generates JSON but it should be possible to create a Builder that generates another format, for instance XML.

The Builder provides the necessary methods to create the AST Nodes:

createObjectLiteral()

createArray()

createObjectReference()

createRegExp(regexp)

createString(string)

createNumber(number)

createBoolean(value)

createNull()

createCustomObject()

These are the JavaScript objects that Mousse supports. In order to create new ones it is necessary to extend the Builder and the Visitor.

To extend the serialization format to know about DOM elements and to serialize them into {"#" "<element id>"} we need to: