- Added
'string'as possiblebigintoption 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"]'- Fixed off by one error using the
maximumBreadthoption. It showed one entry to few being skipped. - Added
safeoption 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 isfalseto 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"}]'- Accept
Array#sort(comparator)comparator method as deterministic option value to use that comparator for sorting object keys.
import { configure } from 'safe-stable-stringify'
const object = {
a: 1,
b: 2,
c: 3,
}
const stringify = configure({
deterministic: (a, b) => b.localeCompare(a)
})
stringify(object)
// '{"c":3,"b":2,"a":1}'- Very minor performance optimization.
Thanks to @flobernd, @cesco69 and @prisis to contribute to this release!
- Fixed toJSON function receiving array keys as number instead of string
- Fixed replacer function receiving array keys as number instead of string
- Fixed replacer function not being called for TypedArray entries
- Improved performance to escape long strings that contain characters that need escaping
- Improved ESM TypeScript types.
- More precise TypeScript replacer type.
- More precise TypeScript types. The return type is now either
string,undefinedorstring | undefineddepending on the input.
- Added
strictoption to verify that the passed in objects are fully compatible with JSON without removing information. If not, an error is thrown. - Fixed TypeScript definition for ESM code bases
- Fix
invalid regexp grouperror in browsers or environments that do not support the negative lookbehind regular expression assertion.
- Accept the
Errorconstructor ascircularValueoption to throw on circular references as the regular JSON.stringify would:
import { configure } from 'safe-stable-stringify'
const object = {}
object.circular = object;
const stringify = configure({ circularValue: TypeError })
stringify(object)
// TypeError: Converting circular structure to JSON- Fixed escaping wrong surrogates. Only lone surrogates are now escaped.
- Reduce module size by removing the test and benchmark files from the published package
- Accept
undefinedascircularValueoption to remove circular properties from the serialized output:
import { configure } from 'safe-stable-stringify'
const object = { array: [] }
object.circular = object;
object.array.push(object)
configure({ circularValue: undefined })(object)
// '{"array":[null]}'- Added
maximumBreadthoption to limit stringification at a specific object or array "width" (number of properties / values) - Added
maximumDepthoption to limit stringification at a specific nesting depth - Implemented the well formed stringify proposal that is now part of the spec
- Fixed maximum spacer length (10)
- Fixed TypeScript definition
- Fixed duplicated array replacer values serialized more than once
- [BREAKING] Convert BigInt to number by default instead of ignoring these values
If you wish to ignore these values similar to earlier versions, just use the new
bigintoption and set it tofalse. - [BREAKING] Support ESM
- [BREAKING] Requires ES6
- Optional BigInt support
- Deterministic behavior is now optional
- The value to indicate a circular structure is now adjustable
- Significantly faster TypedArray stringification
- Smaller Codebase
- Removed stateful indentation to guarantee side-effect freeness
- Fixed an indentation issue in combination with empty arrays and objects
- Updated dev dependencies
- Add support for IE11 (https://github.com/BridgeAR/safe-stable-stringify/commit/917b6128de135a950ec178d66d86b4d772c7656d)
- Fix issue with undefined values (https://github.com/BridgeAR/safe-stable-stringify/commit/4196f87, https://github.com/BridgeAR/safe-stable-stringify/commit/4eab558)
- Fix typescript definition (https://github.com/BridgeAR/safe-stable-stringify/commit/7a87478)
- Improve code coverage (https://github.com/BridgeAR/safe-stable-stringify/commit/ed8cadc, https://github.com/BridgeAR/safe-stable-stringify/commit/b58c494)
- Update dev dependencies (https://github.com/BridgeAR/safe-stable-stringify/commit/b857ea8)
- Improve docs