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

safe-json-stringify

Package Overview
Dependencies
Maintainers
10
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

safe-json-stringify

Prevent defined property getters from throwing errors

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.2M
decreased by-2.1%
Maintainers
10
Weekly downloads
 
Created

What is safe-json-stringify?

The safe-json-stringify package is designed to safely stringify JavaScript objects into JSON format, even when they contain circular references. This package ensures that the JSON.stringify function does not throw errors when encountering such references, making it useful for logging and debugging complex objects.

What are safe-json-stringify's main functionalities?

Safe Stringification

This feature allows you to safely stringify objects that contain circular references. The circular references are replaced with a placeholder string '[Circular]'.

const safeJsonStringify = require('safe-json-stringify');
const obj = { name: 'Alice' };
obj.self = obj;
const jsonString = safeJsonStringify(obj);
console.log(jsonString); // Output: {"name":"Alice","self":"[Circular]"}

Custom Replacer Function

This feature allows you to pass a custom replacer function to filter or modify the values during the stringification process. In this example, the replacer function removes any numeric values from the resulting JSON string.

const safeJsonStringify = require('safe-json-stringify');
const obj = { name: 'Alice', age: 30 };
const replacer = (key, value) => (typeof value === 'number' ? undefined : value);
const jsonString = safeJsonStringify(obj, replacer);
console.log(jsonString); // Output: {"name":"Alice"}

Custom Indentation

This feature allows you to specify a custom indentation level for the resulting JSON string, making it more readable. In this example, the JSON string is formatted with an indentation of 2 spaces.

const safeJsonStringify = require('safe-json-stringify');
const obj = { name: 'Alice', age: 30 };
const jsonString = safeJsonStringify(obj, null, 2);
console.log(jsonString); // Output: {
  "name": "Alice",
  "age": 30
}

Other packages similar to safe-json-stringify

FAQs

Package last updated on 24 May 2018

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