Skip to content

JSON.stringify 遇到 bigint 的类型的数据,会怎么处理?

参考答案:

JSON.stringify() 无法直接序列化 BigInt 类型的数据,如果遇到 BigInt 值,会抛出异常

来看个例子:

js
const obj = { id: 123n };
JSON.stringify(obj);

运行后会报错:

TypeError: Do not know how to serialize a BigInt

原因:

JSON 规范本身并 不支持 BigInt 类型,只支持以下几种数据类型:

  • string
  • number(普通数字)
  • boolean
  • null
  • object
  • array

BigInt 是 ES2020 新增的类型,不属于 JSON 的标准类型。


常见解决方案:

1. 在序列化前手动转换为字符串

js
const obj = { id: 123n };
const json = JSON.stringify(obj, (_, value) =>
  typeof value === 'bigint' ? value.toString() : value
);
console.log(json); // {"id":"123"}

这样序列化后,可以在解析时再手动恢复:

js
const parsed = JSON.parse(json, (_, value) =>
  /^\d+$/.test(value) ? BigInt(value) : value
);
console.log(parsed.id); // 123n

2. 使用第三方库

json-bigint 这样的库可以支持 BigInt 的序列化与反序列化:

bash
npm install json-bigint

示例:

js
const JSONbig = require('json-bigint');
const obj = { id: 123n };

const str = JSONbig.stringify(obj);
console.log(str); // {"id":123}

const parsed = JSONbig.parse(str);
console.log(parsed.id); // 123n

题目要点:

JSON.stringify() 无法直接序列化 BigInt 类型的数据