{"library":"safe-stable-stringify","title":"Safe Stable Stringify","description":"safe-stable-stringify is a JavaScript utility that provides a deterministic and safe alternative to JSON.stringify. Currently at version 2.5.0, it offers consistent object key ordering, graceful handling of circular references, and configurable serialization of BigInt values, addressing common pitfalls of the native `JSON.stringify`. The library maintains a regular release cadence, frequently adding new options and performance improvements. Key differentiators include its configurable deterministic sorting using custom comparators, options to control maximum serialization depth and breadth, and the ability to define how circular references or BigInts are handled (e.g., replacement values, throwing errors, or omission). It ships with TypeScript types, supports both ESM and CommonJS modules, and has zero external dependencies, making it a robust choice for environments requiring reliable JSON serialization.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install safe-stable-stringify"],"cli":null},"imports":["import { stringify } from 'safe-stable-stringify'","import { configure } from 'safe-stable-stringify'","const stringify = require('safe-stable-stringify')"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { configure } from 'safe-stable-stringify';\n\nconst userObject = {\n  id: 123,\n  name: 'Alice',\n  settings: {\n    theme: 'dark',\n    notifications: true,\n    circularRef: null // Will be set later\n  },\n  roles: ['admin', 'user'],\n  creationDate: new Date(),\n  bigIntId: 9007199254740991123n // Example BigInt\n};\nuserObject.settings.circularRef = userObject; // Create a circular reference\n\n// Configure stringify for deterministic output, handling circular refs and BigInts\nconst stringify = configure({\n  deterministic: true, // Sort object keys alphabetically\n  circularValue: '[CIRCULAR_REF]', // Replace circular references with this string\n  bigint: true, // Convert BigInts to numbers\n  maximumDepth: 3 // Limit serialization depth\n});\n\ntry {\n  const serialized = stringify(userObject, null, 2);\n  console.log(serialized);\n  // Expected output (order of keys will be stable, BigInt converted):\n  // {\n  //   \"bigIntId\": 9007199254740991123,\n  //   \"creationDate\": \"2026-04-19T06:51:00.000Z\", // Actual date will vary\n  //   \"id\": 123,\n  //   \"name\": \"Alice\",\n  //   \"roles\": [\n  //     \"admin\",\n  //     \"user\"\n  //   ],\n  //   \"settings\": {\n  //     \"circularRef\": \"[CIRCULAR_REF]\",\n  //     \"notifications\": true,\n  //     \"theme\": \"dark\"\n  //   }\n  // }\n} catch (error) {\n  console.error('Serialization error:', error.message);\n}\n","lang":"typescript","description":"Demonstrates configuring `safe-stable-stringify` to handle circular references, BigInts, and ensure deterministic key order, then serializing an object with these features.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}