Skip to content

Commit

Permalink
mrege upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
qishibo committed Nov 29, 2021
2 parents 7cb82a7 + 2c0a5f8 commit 4ffefbc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
19 changes: 10 additions & 9 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,21 @@ var json_parse = function (options) {
error('Bad number');
} else {
if (BigNumber == null) BigNumber = require('bignumber.js').BigNumber;
//if (number > 9007199254740992 || number < -9007199254740992)
// Bignumber has stricter check: everything with length > 15 digits disallowed
if (string.length > 15)
return _options.storeAsString
? string
: _options.useNativeBigInt
? BigInt(string)
: new BigNumber(string);
else
if (Number.isSafeInteger(number))
return !_options.alwaysParseAsBig
? number
: _options.useNativeBigInt
? BigInt(number)
: new BigNumber(number);
else
// Number with fractional part should be treated as number(double) including big integers in scientific notation, i.e 1.79e+308
return _options.storeAsString
? string
: /[\.eE]/.test(string)
? number
: _options.useNativeBigInt
? BigInt(string)
: new BigNumber(string);
}
},
string = function () {
Expand Down
17 changes: 16 additions & 1 deletion test/bigint-parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("Testing native BigInt support: parse", function () {
console.log('No native BigInt');
return;
}
var input = '{"big":92233720368547758070,"small":123}';
var input = '{"big":92233720368547758070,"small":123,"deci":1234567890.0123456,"shortExp":1.79e+308,"longExp":1.7976931348623157e+308}';

it("Should show JSONbig does support parsing native BigInt", function (done) {
var JSONbig = require('../index')({
Expand All @@ -35,6 +35,21 @@ describe("Testing native BigInt support: parse", function () {
done();
});

it("Should show JSONbig does support decimal and scientific notation parse/stringify roundtrip", function (done) {
var JSONbig = require('../index')({
"useNativeBigInt": true
});
var obj = JSONbig.parse(input);
expect(obj.deci.toString(), "decimal number").to.equal("1234567890.0123456");
expect(typeof obj.deci, "decimal number").to.equal('number');
expect(obj.shortExp.toString(), "short exponential number").to.equal("1.79e+308");
expect(typeof obj.shortExp, "short exponential number").to.equal('number');
expect(obj.longExp.toString(), "long exponential number").to.equal("1.7976931348623157e+308");
expect(typeof obj.longExp, "long exponential number").to.equal('number');
var output = JSONbig.stringify(obj);
expect(output).to.equal(input);
done();
});

it("Should show JSONbig does support native Bigint parse/stringify roundtrip", function (done) {
var JSONbig = require('../index')({
Expand Down

0 comments on commit 4ffefbc

Please sign in to comment.