Skip to content

Latest commit

 

History

History
81 lines (69 loc) · 1.86 KB

README.md

File metadata and controls

81 lines (69 loc) · 1.86 KB

arson Build Status

ARbitrary Structured Object Notation

Not to be confused with the criminal act of deliberately setting fire to property!

JSON is great until you need to encode an object with circular references:

var obj = {};
obj.self = obj;
JSON.stringify(obj); // throws

Throwing an exception is lame, but even worse is muddling along as if everything is ok:

var a = {};
var b = { foo: 42 };
a.x = a.y = b;
var c = JSON.parse(JSON.stringify(a));
assert.strictEqual(c.x, c.y); // fails

We need an object notation that supports circular and repeated references.

That's where ARSON comes in:

var a = {};
var b = { foo: 42 };
a.x = a.y = b;
var c = ARSON.parse(ARSON.stringify(a));
assert.strictEqual(c.x, c.y); // no problem!

ARSON is compact, often even more compact than JSON, because repeated objects are defined only once:

var a = {};
var b = { foo: 42 };
a.x = a.y = b;
ARSON.stringify(a); // [{"x":1,"y":1},{"foo":2},42] vs.
                    // {"x":{"foo":42},"y":{"foo":42}}

But that's not all! ARSON can also encode undefined, thanks to the fact that [][-1] is always undefined:

> ARSON.encode({foo:undefined})
'[{"foo":-1}]'
> ARSON.decode(_)
{ foo: undefined }

It can also encode array holes:

> ARSON.encode(Array(3).concat([4, 5]))
'[[-2,-2,-2,1,2],4,5]'
> ARSON.decode(_)
[ , , , 4, 5 ]

Buffers:

> ARSON.encode(new Buffer("asdf"))
'[["Buffer","YXNkZg==","base64"]]'
> ARSON.decode(_)
<Buffer 61 73 64 66>

Dates:

> ARSON.encode(new Date)
'[["Date","2016-02-02T00:25:36.886Z"]]'
> ARSON.decode(_)
Mon Feb 01 2016 19:25:36 GMT-0500 (EST)

and RegExps:

> ARSON.encode(/asdf/img)
'[["RegExp","asdf","img"]]'
> ARSON.decode(_)
/asdf/gim