const stringify = require('safe-stable-stringify')const bigint = { a: 0, c: 2n, b: 1 }stringify(bigint)// '{"a":0,"b":1,"c":2}'JSON.stringify(bigint)// TypeError: Do not know how to serialize a BigIntconst circular = { b: 1, a: 0 }circular.circular = circularstringify(circular)// '{"a":0,"b":1,"circular":"[Circular: 0]"}'JSON.stringify(circular)// TypeError: Converting circular structure to JSONstringify(circular, ['a', 'b'], 2)
stringify.configure(options)
bigint {boolean|string} If true, bigint values are converted to a
number. If set to 'string', bigint values are serialized as JSON strings.
Otherwise they are ignored. Default:true.
circularValue {string|null|undefined|ErrorConstructor} Defines the value for
circular references. String values are wrapped in double quotes without JSON
escaping. Escape JSON special characters in the value to keep the output valid
JSON. Set to undefined, circular properties are not
serialized (array entries are replaced with null). Set to Error, to throw
on circular references. Default:'[Circular: N]', where N is the
referenced ancestor's depth, starting at 0 for the root. Set to '[Circular]'
to retain the previous default format.
deterministic {boolean|function} If true or a Array#sort(comparator)
comparator method, guarantee a deterministic key order instead of relying on
the insertion order. Default:true.
maximumBreadth {number} Maximum number of entries to serialize per object
(at least one). The serialized output contains information about how many
entries have not been serialized. Ignored properties are counted as well
(e.g., properties with symbol values). Using the array replacer overrules this
option. Default:Infinity
maximumDepth {number} Maximum number of object nesting levels (at least 1)
that will be serialized. Objects at the maximum level are serialized as
'[Object]' and arrays as '[Array]'. Default:Infinity
strict {boolean} Instead of handling any JSON value gracefully, throw an
error in case it may not be represented as JSON (functions, NaN, ...).
Circular values and bigint values throw as well in case either option is not
explicitly defined. Sets and Maps are not detected as well as Symbol keys!
Default:false
safe {boolean} If true, calls to .toJSON(), getters, and replacers that
throw will return the error message in place of the object instead of
throwing. Default:false
Returns: {function} A stringify function with the options applied.
Circular values identify the referenced ancestor's depth as [Circular: N]
(configurable), starting at 0 for the root.
Object keys are sorted instead of using the insertion order (configurable).
BigInt values are stringified as regular number instead of throwing a
TypeError (configurable).
Boxed primitives (e.g., Number(5)) are not unboxed and are handled as
regular object.
Those are the only differences to JSON.stringify(). This is a side effect free
variant and toJSON, replacer and the spacer work the same as
with JSON.stringify().