What is @types/json-stringify-safe?
@types/json-stringify-safe provides TypeScript type definitions for the json-stringify-safe package, which is a JSON.stringify wrapper that handles circular references safely.
What are @types/json-stringify-safe's main functionalities?
Safe JSON Stringification
This feature allows you to safely stringify objects that contain circular references without throwing an error. The code sample demonstrates how to use json-stringify-safe to handle an object with circular references.
const stringifySafe = require('json-stringify-safe');
const circularObj = {};
circularObj.circularRef = circularObj;
circularObj.list = [circularObj, circularObj];
console.log(stringifySafe(circularObj));
Custom Replacer Function
This feature allows you to pass a custom replacer function to modify the behavior of the stringification process. The code sample demonstrates how to exclude a specific key from the final JSON string.
const stringifySafe = require('json-stringify-safe');
const obj = { a: 1, b: 2, c: 3 };
function replacer(key, value) {
if (key === 'b') return undefined;
return value;
}
console.log(stringifySafe(obj, replacer));
Custom Indentation
This feature allows you to specify the number of spaces to use for indentation in the output JSON string. The code sample demonstrates how to format the JSON string with a 2-space indentation.
const stringifySafe = require('json-stringify-safe');
const obj = { a: 1, b: 2, c: 3 };
console.log(stringifySafe(obj, null, 2));
Other packages similar to @types/json-stringify-safe
circular-json
circular-json is another package that provides safe JSON stringification for objects with circular references. It offers similar functionality to json-stringify-safe but uses a different approach to handle circular references.
flatted
flatted is a package that allows you to safely stringify and parse objects with circular references. It provides a different API compared to json-stringify-safe and focuses on both serialization and deserialization of circular structures.
fast-safe-stringify
fast-safe-stringify is a package that offers fast and safe JSON stringification for objects with circular references. It aims to provide better performance compared to json-stringify-safe while maintaining safety.
Installation
npm install --save @types/json-stringify-safe
Summary
This package contains type definitions for json-stringify-safe (https://github.com/isaacs/json-stringify-safe).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/json-stringify-safe.
export = stringify;
declare function stringify(
obj: any,
serializer?: stringify.EntryProcessor | null,
indent?: string | number | null,
decycler?: stringify.EntryProcessor,
): string;
declare namespace stringify {
function getSerialize(serializer: EntryProcessor | null, decycler?: EntryProcessor): EntryProcessor;
type EntryProcessor = (key: string, value: any) => any;
}
Additional Details
- Last updated: Tue, 07 Nov 2023 03:09:37 GMT
- Dependencies: none
Credits
These definitions were written by BendingBender.