json-stable-stringify-without-jsonify vs json-stable-stringify
JSON Stringification Libraries
json-stable-stringify-without-jsonifyjson-stable-stringify
JSON Stringification Libraries

These libraries are designed to provide stable and consistent stringification of JSON objects, ensuring that the output is predictable and can be compared reliably. They are particularly useful in scenarios where JSON data needs to be serialized in a consistent order, such as when generating hashes or signatures for data integrity checks. The main difference between the two packages lies in their handling of circular references and the dependency on the JSONify library.

Npm Package Weekly Downloads Trend
3 Years
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
json-stable-stringify-without-jsonify56,900,10112-19 years agoMIT
json-stable-stringify9,868,3697536.4 kB78 months agoMIT
Feature Comparison: json-stable-stringify-without-jsonify vs json-stable-stringify

Circular Reference Handling

  • json-stable-stringify:

    This package can handle circular references by utilizing the JSONify library, allowing it to serialize complex objects that reference themselves, which is essential for certain data structures.

Dependency Management

  • json-stable-stringify:

    This package requires the JSONify library, which may add complexity to your project due to the need to manage an additional dependency.

Performance

  • json-stable-stringify-without-jsonify:

    This package is generally faster and more lightweight since it does not have the overhead of the JSONify library, making it ideal for simpler data structures.

  • json-stable-stringify:

    The inclusion of JSONify may introduce some overhead, but it provides robust handling of complex data structures, making it suitable for applications where data integrity is critical.

Output Consistency

  • json-stable-stringify-without-jsonify:

    Also guarantees consistent output, but without the additional features provided by JSONify, making it suitable for straightforward use cases.

  • json-stable-stringify:

    Ensures that the output JSON string is consistently ordered based on the keys, which is crucial for scenarios where the order of properties affects the outcome, such as in hashing.

Use Case Suitability

  • json-stable-stringify-without-jsonify:

    Ideal for applications that need a simple, fast, and dependency-free solution for stable JSON stringification without the need for handling circular references.

  • json-stable-stringify:

    Best suited for applications that require comprehensive JSON serialization capabilities, including complex object graphs with circular references.

How to Choose: json-stable-stringify-without-jsonify vs json-stable-stringify
  • json-stable-stringify-without-jsonify:

    Select this package if you prefer a lightweight solution that does not rely on external dependencies and if your data structures do not contain circular references, thus simplifying your project.

  • json-stable-stringify:

    Choose this package if you need a stable stringification process that can handle circular references through the use of the JSONify library, making it suitable for complex data structures that may include such references.

README for json-stable-stringify-without-jsonify

json-stable-stringify

This is the same as https://github.com/substack/json-stable-stringify but it doesn't depend on libraries without licenses (jsonify).

deterministic version of JSON.stringify() so you can get a consistent hash from stringified results

You can also pass in a custom comparison function.

browser support

build status

example

var stringify = require('json-stable-stringify');
var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
console.log(stringify(obj));

output:

{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}

methods

var stringify = require('json-stable-stringify')

var str = stringify(obj, opts)

Return a deterministic stringified string str from the object obj.

options

cmp

If opts is given, you can supply an opts.cmp to have a custom comparison function for object keys. Your function opts.cmp is called with these parameters:

opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue })

For example, to sort on the object key names in reverse order you could write:

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);

which results in the output string:

{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}

Or if you wanted to sort on the object values in reverse order, you could write:

var stringify = require('json-stable-stringify');

var obj = { d: 6, c: 5, b: [{z:3,y:2,x:1},9], a: 10 };
var s = stringify(obj, function (a, b) {
    return a.value < b.value ? 1 : -1;
});
console.log(s);

which outputs:

{"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10}

space

If you specify opts.space, it will indent the output for pretty-printing. Valid values are strings (e.g. {space: \t}) or a number of spaces ({space: 3}).

For example:

var obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } };
var s = stringify(obj, { space: '  ' });
console.log(s);

which outputs:

{
  "a": {
    "and": [
      1,
      2,
      3
    ],
    "foo": "bar"
  },
  "b": 1
}

replacer

The replacer parameter is a function opts.replacer(key, value) that behaves the same as the replacer from the core JSON object.

install

With npm do:

npm install json-stable-stringify

license

MIT