@bugsnag/safe-json-stringify

中文文档
6.1.0发布于 2025/08/14MITTypeScriptCommonJS零依赖近 30 天下载 535.2万

Prevent defined property getters from throwing errors

原始使用文档
文档目录4 节
  1. Installation
  2. Usage
  3. Example
  4. License

@bugsnag/safe-json-stringify

Build Status NPM Version

This is a fork of safe-json-stringify with some opinionated changes, specifically for Bugsnag's use-case:

  • IE8 support. Our JS notifier supports IE8, so all of its dependencies must use IE8 compatible APIs.
  • A hard limit on object depth/breadth. Given a deep/wide enough data structure, the original version would run out of memory or stack allocations. Values are replaced with the string ... in such circumstances.
  • Doesn't replace repeated, but non-circular references (implemented by @MikeRalphson in their fork)
  • Adds the ability to apply key-based filters to specific subtrees

Installation

npm install @bugsnag/safe-json-stringify

Usage

The API is the same as JSON.stringify, with an additional options object:

stringify(obj, [optional replacer], [optional spaces], [options])
  • options.redactedKeys: a list of keys whose value should be replaced with the string [REDACTED]. Keys can be strings for case insensitive matches, or regexes for partial or pattern matches. The array can contain a mixture of both.
  • options.redactedPaths: a list of paths where the redactedKeys option will be applied. The format of these strings are key names separated by . and if the property is an array, it is represented with []. For example: events.[].metaData.

Example

var stringify = require('@bugsnag/safe-json-stringify')
stringify({
  api_key: 'd145b8e5afb56516423bc4d605e45442',
  events: [
    {
      errorMessage: 'Failed load tickets',
      errorClass: 'CheckoutError',
      user: {
        name: 'Jim Bug',
        email: '[email protected]',
        api_key: '245b39ebd3cd3992e85bffc81c045924'
      }
    }
  ]
}, null, 2, {
  redactedKeys: [ 'api_key' ],
  redactedPaths: [ 'events.[].user' ]
})

// yields the following json:
// {
//   "api_key": "d145b8e5afb56516423bc4d605e45442",
//   "events": [
//     {
//       "errorMessage": "Failed load tickets",
//       "errorClass": "CheckoutError",
//       "user": {
//         "name": "Jim Bug",
//         "email": "[email protected]",
//         "api_key": "[REDACTED]"
//       }
//     }
//   ]
// }

License

The original code is licensed with MIT, and so are the modifications.

The MIT License (MIT)

Copyright (c) 2014-2018 Debitoor, Bugsnag

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

版本与发布记录

@bugsnag/safe-json-stringify 共发布 9 个版本,首次发布于 2018/04/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
6.1.0latest2025/08/141.2 年前
6.0.02020/07/066 年前
5.0.02019/10/297 年前
4.0.02019/01/047 年前
3.0.02018/08/228 年前
3.0.0-0prerelease2018/08/208 年前

关于 @bugsnag/safe-json-stringify 的常见问题

@bugsnag/safe-json-stringify 是什么?

Prevent defined property getters from throwing errors

如何安装 @bugsnag/safe-json-stringify?

在项目目录运行 npm install @bugsnag/safe-json-stringify,或使用 pnpm add / yarn add / bun add。

@bugsnag/safe-json-stringify 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bugsnag/safe-json-stringify 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bugsnag/safe-json-stringify 可以商用吗?

@bugsnag/safe-json-stringify 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bugsnag/safe-json-stringify 的下载量有多少?

根据 npm 官方下载统计,@bugsnag/safe-json-stringify 在 2026/10/09 – 2026/11/07 这 30 天内被下载 5,352,178 次,日均约 178,406 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bugsnag/safe-json-stringify 还在维护吗?

最新版本 6.1.0 发布于 2025/08/14(1.2 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/04/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。