safe-stable-stringify
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
Safe-stable-stringify is a robust, efficient, and deterministic serialization alternative to the native JavaScript JSON.stringify method. This npm package is designed to handle potential JSON.stringify pitfalls like circular structures and BigInt values, as well as offering determinism and zero dependencies.
To use safe-stable-stringify in your JavaScript application, first, you must install the library through npm by using the command npm install safe-stable-stringify. Once installed, you can import the stringify function into your JavaScript file and use it to serialize your JavaScript objects. Here's a simple usage example:
const stringify = require('safe-stable-stringify')
const bigint = { a: 0, c: 2n, b: 1 }
console.log(stringify(bigint)) // Outputs: '{"a":0,"b":1,"c":2}'
Besides basic usage, the safe-stable-stringify package allows custom configuration to define the behavior for circular references, BigInt values, and more. Here's an example showcasing these configurations in action:
import { configure } from 'safe-stable-stringify'
const stringify = configure({
bigint: true,
circularValue: 'Magic circle!',
deterministic: false,
maximumDepth: 1,
maximumBreadth: 4
})
const circularObj = {
bigint: 999_999_999_999_999_999n,
circular: 'Circular reference!',
ignored: true,
}
const stringifiedObj = stringify(circularObj, null, 4)
console.log(stringifiedObj)
This will output your object serialized to a string where any circular references are replaced by the string 'Magic circle!', and where the maximum depth and max breadth of the object to be included in the serialized string are defined.
The detailed usage and configuration options of safe-stable-stringify are included within its readme file provided on the GitHub page. This readme file serves as the primary source of documentation, offering plenty of information on how to use the package and configure its various options, including handling of circular structures, BigInt values, object key ordering, and more. The readme also provides performance benchmarks comparing safe-stable-stringify with other known alternatives. Be sure to review this readme on GitHub to fully understand how to make the most out of the safe-stable-stringify package in your project.