# @cardanosolutions/json-bigint 中文文档

规范地址：https://npmcn.com/package/%40cardanosolutions/json-bigint/

版本：1\.1\.0
npm 元数据更新时间：2025-12-17T14:57:30.931Z
中文译文更新时间：2026-11-09T02:46:51.130Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40cardanosolutions/json-bigint
原文对照：https://npmcn.com/package/%40cardanosolutions/json-bigint/?lang=original
项目主页：https://github.com/sidorares/json-bigint#readme
源码仓库：https://github.com/sidorares/json-bigint

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：37.8 kB，6 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- 运行依赖：0 个
- 首次发布：2021-09-17T06:54:17.774Z
- 最新版本发布：2025-12-17T14:57:30.700Z
- 近 30 天下载量：121816（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：4（近 12 个月 1 个）

## 中文简介

JSON.parse 有大意的支持 正确处理科学标记

## 安装

```sh
npm install @cardanosolutions/json-bigint
```

也可以使用：`pnpm add @cardanosolutions/json-bigint`、`yarn add @cardanosolutions/json-bigint`、`bun add @cardanosolutions/json-bigint`

## README

# json- bigint (日语)

[![Build Status](https://secure.travis-ci.org/sidorares/json-bigint.png)](http://travis-ci.org/sidorares/json-bigint) [![NPM](https://nodei.co/npm/json-bigint.png?downloads=true&stars=true)](https://nodei.co/npm/json-bigint/)

JSON.parse/被大意支持. 根据道格拉斯克罗克福德 [JSON.js (英语)](https://github.com/douglascrockford/JSON-js) 软件包和 [大数.js](https://github.com/MikeMcl/bignumber.js) 图书馆.

土著 `Bigint` 最近又加入了联署材料,因此我们增加了一种利用它而不是利用它的办法 `bignumber.js`然而,与土著的解析 `BigInt` 的选项.

虽然大多数JSON解析器假设数字值的精度限制与IEEE 754双倍相同,但JSON规格 _没有_ 说说数字精度 十进制中的任何浮点数(可选的科学)注音都是有效的JSON值. 将可能从IEEE 754整数精度中掉出作为JSON api中的字符串的数值序列化是一个好主意,但 `{ "value" : 9223372036854775807}`例如,仍然是一个有效的RFC4627 JSON字符串,在大多数JS运行时间中,结果是 `JSON.parse` 是该对象 : `{ value: 9223372036854776000 }`

\==========

示例:

```js
var JSONbig = require('json-bigint');

var json = '{ "value" : 9223372036854775807, "v2": 123 }';
console.log('Input:', json);
console.log('');

console.log('node.js built-in JSON:');
var r = JSON.parse(json);
console.log('JSON.parse(input).value : ', r.value.toString());
console.log('JSON.stringify(JSON.parse(input)):', JSON.stringify(r));

console.log('\n\nbig number JSON:');
var r1 = JSONbig.parse(json);
console.log('JSONbig.parse(input).value : ', r1.value.toString());
console.log('JSONbig.stringify(JSONbig.parse(input)):', JSONbig.stringify(r1));
```

输出 :

```
Input: { "value" : 9223372036854775807, "v2": 123 }

node.js built-in JSON:
JSON.parse(input).value :  9223372036854776000
JSON.stringify(JSON.parse(input)): {"value":9223372036854776000,"v2":123}


big number JSON:
JSONbig.parse(input).value :  9223372036854775807
JSONbig.stringify(JSONbig.parse(input)): {"value":9223372036854775807,"v2":123}
```

### 选项

解析器的行为可以通过“ 选项” 来描述

#### 选项。 严格、布尔、默认假

指定解析应“限制”在解析字符串中报告重复键. 默认遵循标准json允许的,并类似JSON的行为. 解析,但将上一个值与上一个指定给重复键相覆盖.

设置选项。 严格 = 真实值会在这种重复键出事时发生故障,从而警告您可能丢失的信息.

示例:

```js
var JSONbig = require('json-bigint');
var JSONstrict = require('json-bigint')({ strict: true });

var dupkeys = '{ "dupkey": "value 1", "dupkey": "value 2"}';
console.log('\n\nDuplicate Key test with both lenient and strict JSON parsing');
console.log('Input:', dupkeys);
var works = JSONbig.parse(dupkeys);
console.log('JSON.parse(dupkeys).dupkey: %s', works.dupkey);
var fails = 'will stay like this';
try {
  fails = JSONstrict.parse(dupkeys);
  console.log('ERROR!! Should never get here');
} catch (e) {
  console.log(
    'Succesfully catched expected exception on duplicate keys: %j',
    e
  );
}
```

产出

```
Duplicate Key test with big number JSON
Input: { "dupkey": "value 1", "dupkey": "value 2"}
JSON.parse(dupkeys).dupkey: value 2
Succesfully catched expected exception on duplicate keys: {"name":"SyntaxError","message":"Duplicate key \"dupkey\"","at":33,"text":"{ \"dupkey\": \"value 1\", \"dupkey\": \"value 2\"}"}

```

#### 选项.storeAsString,布尔,默认虚假

指定是否将 BigInts 作为字符串存储在对象中,而不是默认的大数字.

注意,这是一个危险的行为,因为它打破了能够不改变数据类型而转换回转和forth的默认功能(因为这会将所有的 BigInts 转换为活塞字符串).

示例:

```js
var JSONbig = require('json-bigint');
var JSONbigString = require('json-bigint')({ storeAsString: true });
var key = '{ "key": 1234567890123456789 }';
console.log('\n\nStoring the BigInt as a string, instead of a BigNumber');
console.log('Input:', key);
var withInt = JSONbig.parse(key);
var withString = JSONbigString.parse(key);
console.log(
  'Default type: %s, With option type: %s',
  typeof withInt.key,
  typeof withString.key
);
```

产出

```
Storing the BigInt as a string, instead of a BigNumber
Input: { "key": 1234567890123456789 }
Default type: object, With option type: string

```

#### 选项。使用 NativeBigInt,布尔语,默认虚假

指定解析器是否使用本地 BigInt 而不是大数字 。js

示例:

```js
var JSONbig = require('json-bigint');
var JSONbigNative = require('json-bigint')({ useNativeBigInt: true });
var key = '{ "key": 993143214321423154315154321 }';
console.log(`\n\nStoring the Number as native BigInt, instead of a BigNumber`);
console.log('Input:', key);
var normal = JSONbig.parse(key);
var nativeBigInt = JSONbigNative.parse(key);
console.log(
  'Default type: %s, With option type: %s',
  typeof normal.key,
  typeof nativeBigInt.key
);
```

产出

```
Storing the Number as native BigInt, instead of a BigNumber
Input: { "key": 993143214321423154315154321 }
Default type: object, With option type: bigint

```

#### 选项。总是ParseAsBig,布尔,默认的虚假

指定是否将所有数字作为大数字存储.

注意,这是一种危险的行为,因为它打破了无需数据类型变化即可转换回转和转回的默认功能(因为这会将所有数字转换为-和- stayy Big Number)

示例:

```js
var JSONbig = require('json-bigint');
var JSONbigAlways = require('json-bigint')({ alwaysParseAsBig: true });
var key = '{ "key": 123 }'; // there is no need for BigNumber by default, but we're forcing it
console.log(`\n\nStoring the Number as a BigNumber, instead of a Number`);
console.log('Input:', key);
var normal = JSONbig.parse(key);
var always = JSONbigAlways.parse(key);
console.log(
  'Default type: %s, With option type: %s',
  typeof normal.key,
  typeof always.key
);
```

产出

```
Storing the Number as a BigNumber, instead of a Number
Input: { "key": 123 }
Default type: number, With option type: object

```

如果你想强迫所有的数字被解析为本地 `BigInt` (你可能这样做! ) 否则,任何计算都变成真正的头痛:

```js
var JSONbig = require('json-bigint')({
  alwaysParseAsBig: true,
  useNativeBigInt: true,
});
```

#### 选项. proto Action, boolean, 默认: “ 错误”。 可能的值:"error","ingrore","保有"

#### 选项. 构建器动作,布尔,默认:"error". 可能的值:"error","ingrore","保有"

如何控制 `__proto__` 财务报告和审定财务报表 `constructor` 属性被处理。 如果设定为“ 错误” , 则不允许 和 解析() 调用会丢出一个错误 。 如果设置为“ 忽略” 棱镜和它; 数值会从剖析和对象建筑中跳出 . 如果设置为“ 保存 ” `__proto__` 属性设置。 一个人应该格外小心,并确保任何其他消耗生成的数据的图书馆 不易被原型中毒攻击.

示例:

```js
var JSONbigAlways = require('json-bigint')({ protoAction: 'ignore' });
const user = JSONbig.parse('{ "__proto__": { "admin": true }, "id": 12345 }');
// => result is { id: 12345 }
```

### 链接 :

-   [RFC4627:JavaScript对象标记(JSON)的应用程序/json介质类型](http://www.ietf.org/rfc/rfc4627.txt)
-   [Re:\[Json\] 数字大小的限制?](http://www.ietf.org/mail-archive/web/json/current/msg00297.html)
-   [有没有适当的方法来分析 JSON 大量? (长相相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相相接相接相接相接相接相接相接相相相接相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相](http://stackoverflow.com/questions/18755125/node-js-is-there-any-proper-way-to-parse-json-with-large-numbers-long-bigint)
-   [JavaScript的"麦克斯"是什么? Number在不失去精确度的情况下能达到的最高整数值是什么?](http://stackoverflow.com/questions/307179/what-is-javascripts-max-int-whats-the-highest-integer-value-a-number-can-go-t)
-   [大量数字在 Javascript 中误入四舍五入](http://stackoverflow.com/questions/1379934/large-numbers-erroneously-rounded-in-javascript)

### 关于本地 BigInt 支持的说明

#### 字符串

完全支持出框,将 BigInts 字符串为纯数字( 无引号, 否) `n`(中文(简体) )

#### 限制

-   往返业务

`s === JSONbig.stringify(JSONbig.parse(s))` 不过

`o !== JSONbig.parse(JSONbig.stringify(o))`

何时 `o` 有价值的东西 `123n`。 。 。 .

`JSONbig` 字符串 `123n` 作为 `123`,变成 `number` (亚卡语) `123` 没有 `123n`)在被修复时默认存在.

目前没有一致的方法来处理这个问题,所以我们决定离开它,然后由用户来处理这个具体案件.

## 常见问题

### @cardanosolutions/json-bigint 是什么？

JSON\.parse 有大意的支持 正确处理科学标记

### 如何安装 @cardanosolutions/json-bigint？

在项目目录运行 npm install @cardanosolutions/json-bigint，或使用 pnpm add / yarn add / bun add。

### @cardanosolutions/json-bigint 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/cardanosolutions\_\_json-bigint 是否存在。

### @cardanosolutions/json-bigint 支持 ESM 还是 CommonJS？

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

### @cardanosolutions/json-bigint 可以商用吗？

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

### @cardanosolutions/json-bigint 的下载量有多少？

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

### @cardanosolutions/json-bigint 还在维护吗？

最新版本 1\.1\.0 发布于 2025/12/17（10 个月前）；至今共发布 4 个版本，其中过去 12 个月发布了 1 个；首次发布于 2021/09/17。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

