@apimatic/json-bigint (英语)
JSON.parse/被大意支持. 根据道格拉斯克罗克福德 JSON.js (英语) 软件包。 这是叉子 json- bigint (日语)。本模块对大数.js没有支持,因此取消了该依赖性.
使用 npm 安装库 :
$ npm install @apimatic/json-bigint
虽然大多数JSON解析器假设数字值的精度限制与IEEE 754双倍相同,但JSON规格 没有 说说数字精度 十进制中的任何浮点数(可选的科学)注音都是有效的JSON值. 将可能从IEEE 754整数精度中掉出作为JSON api中的字符串的数值序列化是一个好主意,但 { "value" : 9223372036854775807}例如,仍然是一个有效的RFC4627 JSON字符串,在大多数JS运行时间中,结果是 JSON.parse 是该对象 : { value: 9223372036854776000 }
==========
示例:
var JSONbig = require('@apimatic/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\nbigint 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}
bigint JSON:
JSONbig.parse(input).value : 9223372036854775807
JSONbig.stringify(JSONbig.parse(input)): {"value":9223372036854775807,"v2":123}
选项
解析器的行为可以通过“ 选项” 来描述
选项。 严格、布尔、默认假
指定解析应“限制”在解析字符串中报告重复键. 默认遵循标准json允许的,并类似JSON的行为. 解析,但将上一个值与上一个指定给重复键相覆盖.
设置选项。 严格 = 真实值会在这种重复键出事时发生故障,从而警告您可能丢失的信息.
示例:
var JSONbig = require('@apimatic/json-bigint');
var JSONstrict = require('@apimatic/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 转换为活塞字符串).
示例:
var JSONbig = require('@apimatic/json-bigintt');
var JSONbigString = require('@apimatic/json-bigint')({ storeAsString: true });
var key = '{ "key": 1234567890123456789 }';
console.log('\n\nStoring the BigInt as a string, instead of a BigInt');
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 BigInt
Input: { "key": 1234567890123456789 }
Default type: bigint, With option type: string
选项。总是ParseAsBig,布尔,默认的虚假
指定是否将所有数字存储为 BigInt .
注意,这是一个危险的行为,因为它打破了无需数据类型变化即可转换回转和forth的默认功能(因为这会将所有数字转换为-和- stayy BigInt)
示例:
var JSONbig = require('@apimatic/json-bigint');
var JSONbigAlways = require('@apimatic/json-bigint')({ alwaysParseAsBig: true });
var key = '{ "key": 123 }'; // there is no need for BigInt by default, but we're forcing it
console.log(`\n\nStoring the Number as a BigInt, 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 BigInt, instead of a Number
Input: { "key": 123 }
Default type: number, With option type: bigint
选项. proto Action, boolean, 默认: “ 错误”。 可能的值:"error","ingrore","保有"
选项. 构建器动作,布尔,默认:"error". 可能的值:"error","ingrore","保有"
如何控制 __proto__ 财务报告和审定财务报表 constructor 属性被处理。 如果设定为“ 错误” , 则不允许 和
解析() 调用会丢出一个错误 。 如果设置为“ 忽略” 棱镜和它; 数值会从剖析和对象建筑中跳出 .
如果设置为“ 保存 ” __proto__ 属性设置。 一个人应该格外小心,并确保任何其他消耗生成的数据的图书馆
不易被原型中毒攻击.
示例:
var JSONbigAlways = require('@apimatic/json-bigint')({ protoAction: 'ignore' });
const user = JSONbig.parse('{ "__proto__": { "admin": true }, "id": 12345 }');
// => result is { id: 12345 }
链接 :
- RFC4627:JavaScript对象标记(JSON)的应用程序/json介质类型
- Re:[Json] 数字大小的限制?
- 有没有适当的方法来分析 JSON 大量? (长相相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相接相相接相接相接相接相接相接相接相相相接相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相
- JavaScript的"麦克斯"是什么? Number在不失去精确度的情况下能达到的最高整数值是什么?
- 大量数字在 Javascript 中误入四舍五入
关于本地 BigInt 支持的说明
字符串
完全支持出框,将 BigInts 字符串为纯数字( 无引号, 否) n(中文(简体) )
限制
- 往返业务
s === JSONbig.stringify(JSONbig.parse(s)) 不过
o !== JSONbig.parse(JSONbig.stringify(o))
何时 o 有价值的东西 123n。 。 。 .
JSONbig 字符串 123n 作为 123,变成 number (亚卡语) 123 没有 123n)在被修复时默认存在.
目前没有一致的方法来处理这个问题,所以我们决定离开它,然后由用户来处理这个具体案件.