Skip to content

v3.0.0

Latest

Choose a tag to compare

@BridgeAR BridgeAR released this 15 Oct 16:50
bdd585f

Breaking changes

  • Require Node.js 18 or later (older versions will likely still work but are not supported).
  • Changed the default circular marker to '[Circular: N]', where N is the referenced ancestor's depth, starting at 0 for the root.
    Set circularValue: '[Circular]' to retain the previous format. This changes the serialized output for circular references.

Features

  • Added 'string' as possible bigint option value. Bigint values are serialized as JSON strings using it.
import { configure } from 'safe-stable-stringify'

const stringify = configure({
  bigint: 'string'
})

stringify([1n, 2, 3n, 4, 5n])
// '["1",2,"3",4,"5"]'
  • Added safe option to not fail in case a getter, .toJSON(), or a replacer throws an error.
    Instead, a string as error message is replacing the object inspection. This allows to partially inspect such objects.
    The default is false to prevent any breaking change.
import { configure } from 'safe-stable-stringify'

const stringify = configure({
  safe: true
})

stringify([{
  foo: { a: 5, get foo() { throw new Error('Oops') }, c: true }
}])
// '[{"foo":"Error: Stringification failed. Message: Oops"}]'

stringify([{
  foo: { a: 5, toJSON() { throw new Error('Oops') }, c: true }
}])
// '[{"foo":"Error: Stringification failed. Message: Oops"}]'

Fixes

  • Fixed ESM imports in Node.js and bundlers, including applications that also use CommonJS.
  • Fixed off by one error using the maximumBreadth option. It showed one entry to few being skipped.
  • Fixed throwing on a negative spacer number
  • Fixed serializing non-finite floating-point typed array values (e.g., new Float32Array([Infinity]))

Performance

  • Sorting and escaping is now faster on newer Node.js versions
  • Defer index string conversion