├── .gitignore ├── .hgignore ├── index.js ├── package.json ├── LICENSE-MIT-Cereal ├── README.md ├── test └── unittests.html └── lib └── cereal.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *~ 3 | -------------------------------------------------------------------------------- /.hgignore: -------------------------------------------------------------------------------- 1 | ~$ 2 | ^node_modules/ 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib/cereal'); 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cereal", 3 | "author": "Matthew Sackman", 4 | "version": "0.2.3", 5 | "repository": { 6 | "type": "git", 7 | "url": "git://github.com/atomizejs/cereal.git" 8 | }, 9 | "main": "index", 10 | "description": "Serialisation library for JavaScript that understands object graphs", 11 | "homepage": "http://atomizejs.github.com/" 12 | } 13 | -------------------------------------------------------------------------------- /LICENSE-MIT-Cereal: -------------------------------------------------------------------------------- 1 | Copyright (C) 2011-2012 VMware, Inc. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cereal 2 | 3 | Serialisation Library for JavaScript that respects object aliases, 4 | copes with cycles in the object graph, understands `undefined`, and 5 | can cope with arrays that have arbitrary properties. 6 | 7 | Can be used either client-side or in NodeJS. 8 | 9 | 10 | ## What does it solve? 11 | 12 | ### Aliases 13 | 14 | var x = {}; 15 | var y = {a: x, b: x}; 16 | 17 | If you take the above and then do `JSON.parse(JSON.stringify(y))` then 18 | you will lose the alias to `x`: what you'll get back will be `{a: {}, 19 | b: {}}`. 20 | 21 | If you instead do `Cereal.parse(Cereal.stringify(y))` then you'll get 22 | back the correct object shape, with both `a` and `b` pointing to the 23 | same object. 24 | 25 | ### Loops 26 | 27 | JSON can't cope with cyclical data structures. Cereal can. 28 | 29 | var x = {}; 30 | x.x = x; 31 | 32 | JSON will blow up if you try to `stringify(x)`. Cereal will work 33 | correctly. 34 | 35 | ### Undefined 36 | 37 | JSON can't represent `undefined`. Cereal can. 38 | 39 | var x = {a: undefined}; 40 | 41 | `JSON.parse(JSON.stringify(x))` will yield `{}`. Cereal will get it 42 | right. 43 | 44 | ### Array fields 45 | 46 | JSON thinks arrays can't have arbitrary properties, and will drop 47 | them. Cereal won't. 48 | 49 | var x = [5]; 50 | x.foo = true; 51 | 52 | `JSON.parse(JSON.stringify(x))` will yield `[5]`. Cereal will get it 53 | right. 54 | 55 | 56 | ## Anything else? 57 | 58 | JSON invokes `toJSON` on an object before encoding it. Analogously to 59 | this, Cereal invokes a `cerealise` function if it exists and encodes 60 | what is returned from that. 61 | 62 | Note that Cereal first rewrites the object structure to something 63 | without loops or aliases (but from which the loops and aliases can be 64 | reconstructed) and then it just uses normal JSON encoding on the 65 | result, and vice-versa. 66 | 67 | As a result, Cereal will ignore everything that JSON would ignore 68 | too. Thus as normal, you lose functions, prototypes etc etc. 69 | -------------------------------------------------------------------------------- /test/unittests.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |