[JavaScript] fast-safe-stringify is Convenient for Preventing TypeError: Converting circular structure to JSON
I’ll introduce how using fast-safe-stringify is good for preventing TypeError: Converting circular structure to JSON with JSON.stringify.
When using JSON.stringify in cases like the one below, TypeError: Converting circular structure to JSON occurs.
const o = { a: 1 }
o.o = o
console.log(JSON.stringify(o))
// TypeError: Converting circular structure to JSON
Using fast-safe-stringify instead of JSON.stringify prevents the TypeError: Converting circular structure to JSON and converts it to the string [Circular].
const safeStringify = require('fast-safe-stringify')
const o = { a: 1 }
o.o = o
console.log(safeStringify(o))
// '{"a":1,"o":"[Circular]"}'
While json-stringify-safe exists for the same purpose, benchmarks posted on GitHub show that fast-safe-stringify is faster.
That’s all from the Gemba where I use fast-safe-stringify to avoid encountering TypeError: Converting circular structure to JSON.