Skip to content

safeStringify

Safely stringify object to JSON.

Usage

js
import { safeStringify } from 'js-cool'

Signature

typescript
function safeStringify(data: any, replacer?: any, space?: number): string | undefined

Parameters

ParameterTypeDescription
dataanyData to stringify
replaceranyReplacer function
spacenumberIndentation spaces

Returns

string | undefined - JSON string or undefined on error.

Examples

js
safeStringify({ a: 1 }) // '{"a":1}'
safeStringify({ a: 1 }, null, 2) // '{\n  "a": 1\n}'
safeStringify(circularRef) // undefined (no error thrown)

Released under the MIT License.