Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

safe-stringify

Package Overview
Dependencies
Maintainers
0
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

safe-stringify

Serialize objects to JSON with handling for circular references

  • 1.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16K
decreased by-1.62%
Maintainers
0
Weekly downloads
 
Created
Source

safe-stringify

Serialize objects to JSON with handling for circular references

JSON.stringify() throws an error if the object contains circular references. This package replaces circular references with "[Circular]".

Install

npm install safe-stringify

Usage

import safeStringify from 'safe-stringify';

const foo = {a: true};
foo.b = foo;

console.log(safeStringify(foo));
//=> '{"a":true,"b":"[Circular]"}'

console.log(JSON.stringify(foo));
//=> TypeError: Converting circular structure to JSON

API

safeStringify(value, options?)

Returns a string.

Note: There is no replacer option as I didn't need that, but “pull request welcome” if you need it.

value

Type: unknown

The value to convert to a JSON string.

options

Type: object

indentation

Type: 'string' | 'number'

The indentation of the JSON.

By default, the JSON is not indented. Set it to '\t' for tab indentation or the number of spaces you want.

FAQ

Why another safe stringify package?

The existing ones either did too much, did it incorrectly, or used inefficient code (not using WeakSet). For example, many packages incorrectly replaced all duplicate objects, not just circular references, and did not handle circular arrays.

  • decircular - Remove circular references from objects

Keywords

FAQs

Package last updated on 23 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc