calmer-json
v1.2.0
Published
Safe way to build or parse JSON. Never throws on circular refs and BigInts
Downloads
194
Maintainers
Readme
calmer-json
Safe way to build or parse JSON. Never throws on circular refs and BigInts.
Usage
npm install calmer-jsontoJson — stringify
const text = toJson(value, options?)valueValue to stringify to a JSON textoptions.replacer?Function to transform value entries (if any)options.space?String to use to indent or number of spaces for indentationoptions.onError?Callback to handle possible errors and provide a fallback value
Unlike JSON.stringify, toJson never throws on circular references or BigInt values. It just ignores them as non-serializable:
const example = {
circular: null,
bigint: BigInt('1'),
foo: 'bar',
};
example.circular = example;
toJson(example); // -> '{"foo":"bar"}'fromJson — parse
const value = fromJson(text, options?)textString to parse from JSON text to a valueoptions.reviver?Function to transform value entries (if any)options.onError?Callback to handle possible errors and provide a fallback value
