在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:sidorares/json-bigint开源软件地址:https://github.com/sidorares/json-bigint开源编程语言:JavaScript 100.0%开源软件介绍:json-bigintJSON.parse/stringify with bigints support. Based on Douglas Crockford JSON.js package and bignumber.js library. Native While most JSON parsers assume numeric values have same precision restrictions as IEEE 754 double, JSON specification does not say anything about number precision. Any floating point number in decimal (optionally scientific) notation is valid JSON value. It's a good idea to serialize values which might fall out of IEEE 754 integer precision as strings in your JSON api, but ========== 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:
OptionsThe behaviour of the parser is somewhat configurable through 'options' options.strict, boolean, default falseSpecifies the parsing should be "strict" towards reporting duplicate-keys in the parsed string. The default follows what is allowed in standard json and resembles the behavior of JSON.parse, but overwrites any previous values with the last one assigned to the duplicate-key. Setting options.strict = true will fail-fast on such duplicate-key occurances and thus warn you upfront of possible lost information. 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
options.storeAsString, boolean, default falseSpecifies if BigInts should be stored in the object as a string, rather than the default BigNumber. Note that this is a dangerous behavior as it breaks the default functionality of being able to convert back-and-forth without data type changes (as this will convert all BigInts to be-and-stay strings). 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
options.useNativeBigInt, boolean, default falseSpecifies if parser uses native BigInt instead of 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
options.alwaysParseAsBig, boolean, default falseSpecifies if all numbers should be stored as BigNumber. Note that this is a dangerous behavior as it breaks the default functionality of being able to convert back-and-forth without data type changes (as this will convert all Number to be-and-stay 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
If you want to force all numbers to be parsed as native var JSONbig = require('json-bigint')({
alwaysParseAsBig: true,
useNativeBigInt: true,
}); options.protoAction, boolean, default: "error". Possible values: "error", "ignore", "preserve"options.constructorAction, boolean, default: "error". Possible values: "error", "ignore", "preserve"Controls how example: var JSONbigAlways = require('json-bigint')({ protoAction: 'ignore' });
const user = JSONbig.parse('{ "__proto__": { "admin": true }, "id": 12345 }');
// => result is { id: 12345 } Links:
Note on native BigInt supportStringifyingFull support out-of-the-box, stringifies BigInts as pure numbers (no quotes, no Limitations
when
There is currently no consistent way to deal with this issue, so we decided to leave it, handling this specific case is then up to users. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论