CANON
CANON is canonical object notation. It closely resembles JSON. In fact,
CANON.stringify
and CANON.parse
make use of their JSON
counterparts
internally.
What's wrong with JSON?
JSON is great for passing around serialized data. There's a second reason one might wish to serialize data, though: to implement efficient sets and dictionaries, two useful data structures JavaScript currently lacks.
In order to implements sets and dictionaries efficiently, one needs to be
able to hash values consistently. JSON.stringify
does not guarantee the
order of object keys, so cannot be relied upon.
Implementing sets with CANON
The only data structure JavaScript currently provides for dealing with unique collections is the humble object. Only strings can be used as keys, though, so it's necessary to serialize each value that's added to the set. This yields a data structure mapping serialized values to the values themselves:
CANON.stringify(value1) ➞ value1CANON.stringify(value2) ➞ value2...CANON.stringify(valueN) ➞ valueN
To limit the length of the keys (and thus the memory footprint), a hashing function can be used:
sha256(CANON.stringify(value1)) ➞ value1sha256(CANON.stringify(value2)) ➞ value2...sha256(CANON.stringify(valueN)) ➞ valueN
A simple set implementation might resemble the following:
= sha256 CANONstringify value : @values = @add values... : Object::hasOwnPropertycall @valueshash value : for value in values @valueshash value= value return : for value in values delete @valueshash value return : for own keyvalue of @values iterator value return
coffee> points = 125236 values: '736e4ff990cbad3e9ed1b2d78abfea3bd73a5e773960f40fbbc42e490df999bf': 12 '41cc5c39058d6626dfa57703740a21676229901e1a26f844fc96cb7462e05828': 52 'cd326a88a511fc5ca7831944f0f2a3091273faf7e5fbec3f8e482ace48392657': 36 coffee> pointscontains 44falsecoffee> pointscontains 52truecoffee> pointseach consolelog point 12 52 36 undefined
Differences between CANON and JSON
> CANON > JSON'-0' '0'> CANON > JSON'["Array",1,2,3]' '[1,2,3]'> CANON > JSON'["Date","2012-10-14T20:27:37.000Z"]' '"2012-10-14T20:27:37.000Z"'> CANON > JSON'["Number","Infinity"]' 'null'> CANON > JSON'["Number","-Infinity"]' 'null'> CANON > JSON'["Number","NaN"]' 'null'> CANON > JSON'["Object","bar",2,"foo",1]' '{"foo":1,"bar":2}'> CANON > JSON'["RegExp","/foo/i"]' '{}'> CANON > JSON'["Undefined"]' undefined> CANON > JSONTypeError: Functions cannot be serialized undefined
From the output of JSON.stringify
it's not always possible to determine the
input value:
> JSON === JSONtrue
Since CANON.stringify
includes type information for most values, different
values with the same string representation (such as /foo/i
and '/foo/i'
)
are serialized differently. As a result, CANON.parse
can materialize Date
and RegExp objects:
> CANON instanceof Datetrue> JSON instanceof Datefalse
Installation
Browser:
Server:
$ npm install canon
Running the test suite
$ make setup$ make test
To run the test suite in a browser, open test/index.html.