Home

Awesome

ResurrectJS

ResurrectJS preserves object behavior (prototypes) and reference circularity with a special JSON encoding. Unlike flat JSON, it can also properly resurrect these types of values:

Supported Browsers:

Read about how it works.

Examples

function Foo() {}
Foo.prototype.greet = function() { return "hello"; };

// Behavior is preserved:
var necromancer = new Resurrect();
var json = necromancer.stringify(new Foo());
var foo = necromancer.resurrect(json);
foo.greet();  // => "hello"

// References to the same object are preserved:
json = necromancer.stringify([foo, foo]);
var array = necromancer.resurrect(json);
array[0] === array[1];  // => true
array[1].greet();  // => "hello"

// Dates are restored properly
json = necromancer.stringify(new Date());
var date = necromancer.resurrect(json);
Object.prototype.toString.call(date);  // => "[object Date]"

Options

Options are provided to the constructor as an object with these properties:

For example,

var necromancer = new Resurrect({
    prefix: '__#',
    cleanup: true
});

Methods

Only two methods are significant when using ResurrectJS.

Restrictions

With the default resolver, all constructors must be named and stored in the global variable under that name. This is required so that the prototypes can be looked up and reconnected at resurrection time.

The wrapper objects Boolean, String, and Number will be unwrapped. This means extra properties added to these objects will not be preserved.

Functions cannot ever be serialized. Resurrect will throw an error if a function is found when traversing a data structure.

Custom Resolvers

There is a caveat with the provided resolver, NamespaceResolver: all constructors must be explicitly named when defined. For example, see the Foo constructor in this example,

var namespace = {};
namespace.Foo = function Foo() {
    this.bar = true;
};
var necromancer = new Resurrect({
    resolver: new Resurrect.NamespaceResolver(namespace)
});

The constructor been assigned to the Foo property and the function itself has been given a matching name. This is how the resolver will find the name of the constructor in the namespace when given the constructor. Keep in mind that using this form will bind the variable Foo to the surrounding function within the body of Foo.

See Also