calmer-json
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

calmer-json

Like JSON stringify/parse, but never throws on circular references and BigInt values.

Usage

npm install calmer-json

toJson — stringify

const text = toJson(value, options?)
  • value Value to stringify to a JSON text
  • options.replacer? Function to transform value entries (if any)
  • options.space? String to use to ident or number of spaces for identation
  • options.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?)
  • text String to parse from JSON text to a value
  • options.reviver? Function to transform value entries (if any)
  • options.onError? Callback to handle possible errors and provide a fallback value

Package Sidebar

Install

npm i calmer-json

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

5.45 kB

Total Files

5

Last publish

Collaborators

  • icmx