a canonical json implementation
npm install canonical-json
|141||downloads in the last day|
|997||downloads in the last week|
|3 849||downloads in the last month|
|Version||0.0.4 last updated a month ago|
The goal of this module is to implement a version of JSON.stringify that returns a canonical JSON format.
Canonical JSON means that the same object should always be stringified to the exact same string.
Properties of non-array objects are not guaranteed to be stringified in any particular order. Do not rely on ordering of properties within the same object within the stringification.
This module implements two alternative solutions to this problem:
- index.js is based on Douglas Crockford's json2.js. I modified it to serialize object keys sorted on the fly.
- index2.js recursively creates a copy of the object to sort its keys. The copy is then simply passed to native JSON.stringify
It currently exports the index.js version.
I compared the performance of native JSON.stringify and the two alternative implementations that output keys sorted:
- native JSON.stringify:
- js JSON.stringify with sorted keys (implementation):
- copy and native JSON.stringify with sorted keys (implementation:
The tests were run in Node.js on a 2011 MacBook Pro.
Performance test source: test/performance.js