@danor-lib/json-bigint
v2.0.3
Published
JSON parse and stringify with native BigInt and ESM support in modern JavaScript
Downloads
437
Readme
[!TIP] 本文档由 AI 阅读代码后生成。中文版本已经过本人初步审校。英文版本基于中文版本由 AI 翻译生成。如有问题或更好的文档,欢迎提交 Issue。
This document is generated by AI after reading the code. The Chinese version has been preliminarily reviewed by the author. The English version is AI-translated based on the Chinese version. If you have any issues or a better version of the documentation, please feel free to submit an Issue.
@danor-lib/json-bigint
一个轻量级的 JSON 解析与序列化库,原生支持 BigInt。
A lightweight JSON parser and serializer with native BigInt support.
基础示例
import { parse, stringify } from '@danor-lib/json-bigint';
const input = '{"big":9223372036854775807}';
const object = parse(input);
console.log(typeof object.big); // 'bigint'
console.log(object.big.toString()); // '9223372036854775807'
const output = stringify(object);
console.log(output); // '{"big":9223372036854775807}'静态方法
parse(text, reviver?, option?)
将 JSON 字符串解析为 JavaScript 值。该函数会自动识别超出安全整数范围的整数,并将其转换为 BigInt。
- 参数:
text{string}- 要解析的 JSON 文本。reviver{function}可选- 与原生JSON.parse相同的转换器函数。option{ParseOption}可选- 解析选项。
ParseOption说明
| 属性 | 类型 | 默认 | 描述 |
| --------------------- | ----------------------------------- | --------- | --------------------------------------------------------- |
| preferParseAsBigInt | boolean | false | 对安全整数范围内的数字也优先解析为 BigInt。 |
| preferBigIntString | boolean | false | 对超出安全整数范围的数字返回原始字符串,而不是 BigInt。 |
| protoAction | 'error' \| 'ignore' \| 'preserve' | 'error' | 解析对象键名为 __proto__ 时的处理方式。 |
| constructorAction | 'error' \| 'ignore' \| 'preserve' | 'error' | 解析对象键名为 constructor 时的处理方式。 |
stringify(value, replacer?, space?)
将 JavaScript 值序列化为 JSON 字符串。支持 BigInt 类型,并保留 toJSON、replacer 以及缩进选项。
- 参数:
value{any}- 要序列化的值。replacer{function|Array}可选- 与原生JSON.stringify相同的替换器。space{string|number}可选- 缩进空格数或缩进字符串。
错误代码
| 代码 | 位置 | 描述 | 上下文数据 |
| ------------------------------------- | --------------------------------- | ------------------------------------------------------------------------ | ----------------------------- |
| invalid-type-option-protoAction | json-bigint/parse(3:options) | protoAction 选项值无效,仅允许 'error' 'ignore' 'preserve' | { protoAction, option } |
| invalid-type-option-constructorAction | json-bigint/parse(3:options) | constructorAction 选项值无效,仅允许 'error' 'ignore' 'preserve' | { constructorAction, option } |
| contain-forbidden-prototype | json-bigint/parse | 对象包含禁止的 __proto__ 键 | { key, index } |
| contain-forbidden-constructor | json-bigint/parse | 对象包含禁止的 constructor 键 | { key, index } |
| bad-number | json-bigint/parse | 数字格式无效或为非有限数 | { string, index } |
| bad-string | json-bigint/parse | 字符串格式无效(缺少闭合引号或转义错误) | { string, index } |
| bad-array | json-bigint/parse | 数组格式无效 | { string, index } |
| bad-object | json-bigint/parse | 对象格式无效 | { string, index } |
| unexpected-char | json-bigint/parse | 解析时期望匹配某个字符但遇到了其他字符 | { char, index, charExpected } |
| unexpected-word | json-bigint/parse | 遇到无法识别的字面量标记 | { char, index } |
| invalid-replacer | json-bigint/stringify(2:replacer) | replacer 参数类型无效(必须为函数或数组) | { value } |
