json-stable-stringify-without-jsonify
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The npm package json-stable-stringify-without-jsonify serves as a deterministic version of JSON.stringify()
. It allows users to generate a consistent hash from stringified results. This particular variant does not depend on libraries without licenses, differentiating it from other versions such as the one found at substack/json-stable-stringify. It also supports the inclusion of a custom comparison function for object keys, introducing an added layer of customization.
To use json-stable-stringify-without-jsonify, begin by installing the npm package with npm install json-stable-stringify
. Once installed, require the package in your JavaScript file. For instance, var stringify = require('json-stable-stringify');
allows you to utilize its functionalities.
For a basic use case, consider an object { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
. Using the stringify method, console.log(stringify(obj));
, the output will be a deterministic stringified string of the object: {"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}
.
For more advanced usage, you can supply options to have a custom comparison function for object keys, or modify the output for pretty-printing by specifying opts.space
.
Example with a custom comparison function:
var stringify = require('json-stable-stringify');
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
var s = stringify(obj, function (a, b) {
return a.key < b.key ? 1 : -1;
});
console.log(s);
// Output: {"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}
Example with pretty-printing:
var obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } };
var s = stringify(obj, { space: ' ' });
console.log(s);
// Output:
// {
// "a": {
// "and": [
// 1,
// 2,
// 3
// ],
// "foo": "bar"
// },
// "b": 1
// }
The documentation for json-stable-stringify-without-jsonify can be found within the readme file in the package's GitHub repository. Users can learn about the methods and options as well as installation instructions and usage examples straight from the readme. An additional resource is the npm documentation page. The MIT License under which the package is released can also be reviewed in the readme.