Easily serialize and deserialize JavaScript objects.
Built for node.js and browsers. Cryo is inspired by Python's pickle and works similarly to JSON.stringify() and JSON.parse(). Cryo.stringify() and Cryo.parse() handle these additional circumstances:
$ npm install cryo
Add the latest minified build to your project as a script:
<script type='text/javascript' src='cryo-0.0.3.min.js'></script>
var Cryo = require('cryo');
var obj = {
name: 'Hunter',
hello: function() {
console.log(this.name + ' says hello!');
}
};
var frozen = Cryo.stringify(obj);
var hydrated = Cryo.parse(frozen);
hydrated.hello(); // Hunter says hello!
JSON.stringify()
loses properties that are undefined
.
Cryo takes a verbatim snapshot of all your properties, including those that are undefined
.
Cryo successfully works with Date
objects, which JSON.stringify()
mangles into strings.
var Cryo = require('../lib/cryo');
var now = new Date();
var withJSON = JSON.parse(JSON.stringify(now));
console.log(withJSON instanceof Date); // false
var withCryo = Cryo.parse(Cryo.stringify(now));
console.log(withCryo instanceof Date); // true
JSON.stringify()
makes multiple copies of single objects, losing object relationships.
When several references to the same object are stringified with JSON, those references are turned into clones of each other.
Cryo maintains object references so the restored objects are identical to the originals.
This is easier to understand with an example:
var Cryo = require('../lib/cryo');
var userList = [{ name: 'Abe' }, { name: 'Bob' }, { name: 'Carl' }];
var state = {
users: userList,
activeUser: userList[1]
};
var withJSON = JSON.parse(JSON.stringify(state));
console.log(withJSON.activeUser === withJSON.users[1]); // false
var withCryo = Cryo.parse(Cryo.stringify(state));
console.log(withCryo.activeUser === withCryo.users[1]); // true
JSON.stringify(Infinity)
returns null
, even though Infinity
is a numeric type in JavaScript.
Cryo successfully stringifies and parses Infinity
as a Number
.
Tests require node.js.
$ git clone git://github.com/hunterloftis/cryo.git
$ cd cryo
$ make setup
$ make test