json-stable-stringify-without-jsonify

中文文档
1.0.1发布于 2016/12/16MITCommonJS零依赖近 30 天下载 5.5亿

deterministic JSON.stringify() with custom sorting to get deterministic hashes from stringified results, with no public domain dependencies

原始使用文档
文档目录5 节
  1. var str = stringify(obj, opts)
  2. options
  3. cmp
  4. space
  5. replacer

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

版本与发布记录

json-stable-stringify-without-jsonify 共发布 1 个版本,首次发布于 2016/12/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.1latest2016/12/169 年前

关于 json-stable-stringify-without-jsonify 的常见问题

json-stable-stringify-without-jsonify 是什么?

deterministic JSON.stringify() with custom sorting to get deterministic hashes from stringified results, with no public domain dependencies

如何安装 json-stable-stringify-without-jsonify?

在项目目录运行 npm install json-stable-stringify-without-jsonify,或使用 pnpm add / yarn add / bun add。

json-stable-stringify-without-jsonify 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/json-stable-stringify-without-jsonify 是否存在。

json-stable-stringify-without-jsonify 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

json-stable-stringify-without-jsonify 可以商用吗?

json-stable-stringify-without-jsonify 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

json-stable-stringify-without-jsonify 的下载量有多少?

根据 npm 官方下载统计,json-stable-stringify-without-jsonify 在 2026/10/05 – 2026/11/03 这 30 天内被下载 546,460,943 次,日均约 18,215,365 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

json-stable-stringify-without-jsonify 还在维护吗?

最新版本 1.0.1 发布于 2016/12/16(9 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2016/12/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。