json-bigint 分手后的思念是犯贱 2022-11-25 10:27 201阅读 0赞 bigints支持**JSON.parse/stringify**解析方式。基于Douglas Crockford的JSON.js包和bignumber.js库。 本地Bigint最近被添加到JS中,所以我们增加了一个选项来代替bignumber.js。但是,使用本机BigInt进行解析是为了向后兼容 虽然大多数JSON解析器假设数值具有与IEEE 754 double相同的精度限制,但JSON规范对数字精度没有任何规定。任何十进制(可选科学)表示法中的浮点数都是有效的JSON值。在JSON api中,将可能不符合IEEE 754整数精度的值序列化为字符串是一个好主意,例如\{“value”:9223372036854775807\},仍然是一个有效的RFC4627 JSON字符串,在大多数JS运行时是JSON.parse是这个对象:\{value: 9223372036854776000\} = = = = = = = = = = example: 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)); Output: 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} **Options** 解析器的行为可以通过“Options”进行配置 **options.strict, boolean值, 默认值:false** 指定解析应 “strict” 报告已解析字符串中的重复键。默认值遵循标准json中允许的值,类似于json的行为解析,但是用分配给重复键的最后一个值覆盖前面的任何值。 设置options.strict = true 会在重复密钥出现时快速失效,并因此提前警告您可能丢失的信息。 example: 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 ); } Output 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\"}"} **options.storeAsString, boolean值, 默认值:false** 指定BigInts是否应该以字符串的形式存储在对象中,而不是默认的BigNumber。 请注意,这是一种危险的行为,因为它破坏了在不更改数据类型的情况下来回转换的默认功能(因为这将把所有bigint转换为be-and-stay字符串)。 example: 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 ); Output Storing the BigInt as a string, instead of a BigNumber Input: { "key": 1234567890123456789 } Default type: object, With option type: string **options.useNativeBigInt, boolean值, 默认值:false** 指定解析器是否使用本机BigInt而不是bignumber.js example: 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 ); Output Storing the Number as native BigInt, instead of a BigNumber Input: { "key": 993143214321423154315154321 } Default type: object, With option type: bigint **options.alwaysParseAsBig, boolean值, 默认值:false** 指定是否所有数字都应该存储为大数字。 请注意,这是一种危险的行为,因为它破坏了在不更改数据类型的情况下来回转换的默认功能(因为这将把所有数字转换为和保持BigNumber) example: 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 ); Output Storing the Number as a BigNumber, instead of a Number Input: { "key": 123 } Default type: number, With option type: object 如果您希望强制将所有数字解析为本地BigInt(您可能会这样做!)否则,任何计算都会成为真正的头痛): var JSONbig = require('json-bigint')({ alwaysParseAsBig: true, useNativeBigInt: true, }); **options.protoAction,boolean值,默认值:error,还可以设置为 “error”,“ignore”,“preserve”** **options.constructorAction,boolean值,默认值:error,还可以设置为 “error”,“ignore”,“preserve”** 控制\_\_proto\_\_和constructor 的性质如何处理。如果设置为“error”,则不允许使用,parse()调用将抛出一个错误。如果设置为“ignore”,则从解析和对象构建中跳过prroperty的值。如果设置为“preserve”,则设置\_\_proto\_\_属性。应该格外小心,确保使用其他库生成的数据不会受到原型中毒攻击的伤害。 example: var JSONbigAlways = require('json-bigint')({ protoAction: 'ignore' }); const user = JSONbig.parse('{ "__proto__": { "admin": true }, "id": 12345 }'); // => result is { id: 12345 }
还没有评论,来说两句吧...