json-stringify-deterministic vs json-stable-stringify vs json-stable-stringify-without-jsonify
JSON文字列化ライブラリ
json-stringify-deterministicjson-stable-stringifyjson-stable-stringify-without-jsonify

JSON文字列化ライブラリ

これらのライブラリは、JavaScriptオブジェクトをJSON文字列に変換する際に、特定の順序でプロパティを保持することを目的としています。これは、オブジェクトの順序が重要な場合、例えば、ハッシュの衝突を避けるためや、データの一貫性を保つために役立ちます。これにより、同じオブジェクトから生成されるJSON文字列が常に同じになることが保証されます。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
json-stringify-deterministic103,4564111.6 kB02年前MIT
json-stable-stringify07836.4 kB71年前MIT
json-stable-stringify-without-jsonify013-19年前MIT

機能比較: json-stringify-deterministic vs json-stable-stringify vs json-stable-stringify-without-jsonify

文字列化の安定性

  • json-stringify-deterministic:

    このライブラリは、デフォルトのJSON.stringifyの動作を維持しつつ、安定した文字列化を提供します。これにより、既存のコードに対する影響を最小限に抑えつつ、安定性を確保できます。

  • json-stable-stringify:

    このライブラリは、オブジェクトのプロパティをアルファベット順にソートして文字列化します。これにより、同じオブジェクトから生成されるJSON文字列が常に同じになることが保証されます。

  • json-stable-stringify-without-jsonify:

    このライブラリもプロパティを安定した順序でソートしますが、JSON.stringifyを使用しないため、特定の環境での互換性が向上します。

パフォーマンス

  • json-stringify-deterministic:

    このライブラリは、既存のJSON.stringifyのパフォーマンスを維持しますが、安定性を提供するために追加のオプションを提供します。

  • json-stable-stringify:

    このライブラリは、プロパティをソートするために追加の処理を行うため、パフォーマンスに影響を与える可能性がありますが、安定性を優先する場合にはその価値があります。

  • json-stable-stringify-without-jsonify:

    JSON.stringifyを使用しないため、特定のシナリオではパフォーマンスが向上する可能性がありますが、全体的なパフォーマンスは使用するデータによって異なります。

互換性

  • json-stringify-deterministic:

    このライブラリは、JSON.stringifyのデフォルトの動作を変更しないため、既存のコードとの互換性が高いです。

  • json-stable-stringify:

    このライブラリは、広く使用されているため、さまざまな環境での互換性が高いです。

  • json-stable-stringify-without-jsonify:

    JSON.stringifyを使用しないため、特定の環境や要件での互換性が向上します。

使いやすさ

  • json-stringify-deterministic:

    既存のJSON.stringifyの使い方を維持しつつ、安定性を追加するため、学習コストは低いです。

  • json-stable-stringify:

    APIはシンプルで、すぐに使い始めることができます。

  • json-stable-stringify-without-jsonify:

    使いやすさは高いですが、JSON.stringifyを使用しないため、特定のシナリオでは注意が必要です。

設計原則

  • json-stringify-deterministic:

    デフォルトのJSON.stringifyの動作を尊重しつつ、安定性を提供することを目的としています。

  • json-stable-stringify:

    このライブラリは、オブジェクトのプロパティの順序を保証するために設計されています。

  • json-stable-stringify-without-jsonify:

    設計はシンプルで、特定の要件に応じて柔軟性を持たせています。

選び方: json-stringify-deterministic vs json-stable-stringify vs json-stable-stringify-without-jsonify

  • json-stringify-deterministic:

    このライブラリは、JSON.stringifyのデフォルトの動作を変更せずに、安定した文字列化を行いたい場合に選択してください。特に、既存のコードベースに対して最小限の変更を加えたい場合に適しています。

  • json-stable-stringify:

    このライブラリは、オブジェクトのプロパティを安定した順序で文字列化する必要がある場合に選択してください。特に、オブジェクトの順序が重要な場合や、同じオブジェクトから生成される文字列が常に同じであることを保証したい場合に適しています。

  • json-stable-stringify-without-jsonify:

    このライブラリは、JSON.stringifyを使用せずに安定した文字列化を行いたい場合に選択してください。これは、特定の環境や要件でJSON.stringifyが使用できない場合に便利です。

json-stringify-deterministic のREADME

json-stringify-deterministic

Last version Coverage Status NPM Status

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

Similar to json-stable-stringify but:

  • No Dependencies. Minimal as possible.
  • Better cycles detection.
  • Support serialization for object without .toJSON (such as RegExp).
  • Provides built-in TypeScript declarations.

Install

npm install json-stringify-deterministic --save

Usage

const stringify = require('json-stringify-deterministic')
const obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 }

console.log(stringify(obj))
// => {"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}

API

stringify(<obj>, [opts])

obj

Required
Type: object

The input object to be serialized.

opts

opts.stringify

Type: function Default: JSON.stringify

Determinate how to stringify primitives values.

opts.cycles

Type: boolean Default: false

Determinate how to resolve cycles.

Under true, when a cycle is detected, [Circular] will be inserted in the node.

opts.compare

Type: function

Custom comparison function for object keys.

Your function opts.compare 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:

const stringify = require('json-stringify-deterministic')

const obj = { c: 8, b: [{z: 6,y: 5,x: 4}, 7], a: 3 }
const objSerializer = stringify(obj, function (a, b) {
  return a.key < b.key ? 1 : -1
})

console.log(objSerializer)
// => {"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:

const stringify = require('json-stringify-deterministic')

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

console.log(objtSerializer)
// => {"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10}
opts.space

Type: string
Default: ''

If you specify opts.space, it will indent the output for pretty-printing.

Valid values are strings (e.g. {space: \t}). For example:

const stringify = require('json-stringify-deterministic')

const obj = { b: 1, a: { foo: 'bar', and: [1, 2, 3] } }
const objSerializer = stringify(obj, { space: '  ' })
console.log(objSerializer)
// => {
//   "a": {
//     "and": [
//       1,
//       2,
//       3
//     ],
//     "foo": "bar"
//   },
//   "b": 1
// }
opts.replacer

Type: function

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

Related

License

MIT © Kiko Beats.