JSON Normalize
Stringifies objects in a normalized way for use in caching or comparing JSON.
Why?
Key ordering is rendered "as is" when using JSON.stringify.
This makes it impractical to use stringified values for hashing or caching.
// Using JSON.stringifyJSON; // => {"foo":"bar","hello":"world"}JSON; // => {"hello":"world","foo":"bar"}
JSONNormalize
stringifies objects in a "normalized" way by sorting object keys to produce
the same JSON string every time.
// Using JSON Normalizeconst JSONNormalize = ; JSONNormalize; JSONNormalize;
API
JSONNormalize.stringify
Stringifies objects in a normalized way.
Given an object with any key order, the same string will be returned if the objects are the "equivalent".
JSONNormalize.stringify(value[, replacer], callback)
Parameters
value {any}
The value to "stringify".
replacer {function=}
Eqivalent to the replacer parameter JSON.stringify has.
callback {function}
Invoked with two arguments: error and results.
Returns
{undefined}
Example
const JSONNormalize = ; JSONNormalize; JSONNormalize;
JSONNormalize.stringifySync
Syncronous version of JSONNormalize.stringify
JSONNormalize.stringify(value[, replacer], callback)
Parameters
value {any}
The value to "stringify".
replacer {function=}
Eqivalent to the replacer parameter JSON.stringify has.
Returns
{string} A valid JSON string.
Example
const JSONNormalize = ; const results = JSONNormalize;console; // Prints: [{"x":4,"y":3},{"x":5,"y":7},{"x":4,"y":2}] const results = JSONNormalize;console; // Prints: [{"x":4,"y":3},{"x":5,"y":7},{"x":4,"y":2}]
JSONNormalize.normalize
An alias for JSONNormalize.stringify.
JSONNormalize.normalize(value[, replacer], callback)
Parameters
value {any}
The value to "stringify".
replacer {function=}
Eqivalent to the replacer parameter JSON.stringify has.
callback {function}
Invoked with two arguments: error and results.
Returns
{undefined}
Example
const JSONNormalize = ; JSONNormalize;
JSONNormalize.normalizeSync
An alias for JSONNormalize.stringifySync.
JSONNormalize.normalize(value[, replacer], callback)
Parameters
value {any}
The value to "stringify".
replacer {function=}
Eqivalent to the replacer parameter JSON.stringify has.
callback {function}
Invoked with two arguments: error and results.
Returns
{string} A valid JSON string.
Example
const JSONNormalize = ; const results = JSONNormalize; // Do something with results...
Note: The rest of the functions are convenience functions!
They're wrappers around node's crypto
module that take the given object,
JSONNormalize.normalize
it, and then pipe it to crypto.createHash
.
JSONNormalize.md5
Gets the md5 hash for the given object.
JSONNormalize.md5(value, callback)
Parameters
value {any}
The value to get the md5 hash of.
callback {function}
Invoked with two arguments: error and results.
Returns
{undefined}
Example
const objectMD5 = md5;; ;
JSONNormalize.sha256
Gets the sha256 hash for the given object.
JSONNormalize.sha256(value, callback)
Parameters
value {any}
The value to get the sha256 hash of.
callback {function}
Invoked with two arguments: error and results.
Returns
{undefined}
Example
const objectSHA256 = sha256; ; ;
JSONNormalize.sha512
Gets the sha512 hash for the given object.
JSONNormalize.sha512(value, callback)
Parameters
value {any}
The value to get the sha512 hash of.
callback {function}
Invoked with two arguments: error and results.
Returns
{undefined}
Example
const objectSHA512 = sha512; ; ;
JSONNormalize.md5Sync
Syncronous version of JSONNormalize.md5
JSONNormalize.sha256Sync
Syncronous version of JSONNormalize.sha256
JSONNormalize.sha512Sync
Syncronous version of JSONNormalize.md5
All methods have an async equivalent that returns a promise (via Bluebird)
For example, JSONNormalize.stringify's promisified version is JSONNormalize.stringifyAsync
const stringifyAsync = stringifyAsync;cosnt myObject = foo: 'bar' ; // Using promises ; // Even better with async/awaitasync { const results = await ;};
A Practical Use Case
Using objects as cache keys
;; /** * Stores database records for quick lookup. * @type */const cache = {}; /** * Gets a user record from the database with the properties provided in the object "data". * @param * @returns */ { // Argument for data could contain id, name, username, etc. const key = await ; const cached = cachekey; // Cached user record found, return it. if cached return cached; // No cache found, do some time expensive database lookup const results = await db; cachekey = results; return results;}
A (not so) Practical Use Case
Comparing JSON files
foo.json
bar.json
;; /** * Compares the list of object arguments and checks for equivalency (===). * @param * @returns */ { const normalized = await Promiseallobjects; return normalized;} /** * Checks that the given list of filepath arguments contain "equivalent" json. * @param * @returns */ { const objects = await Promiseallpaths; return await ;} async { const filesAreEqual = await ; console; // Prints: true};