Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Non-integers were always parsed as strings, if storeAsString #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,21 +206,29 @@ var json_parse = function (options) {
error('Bad number');
} else {
if (BigNumber == null) BigNumber = require('bignumber.js');
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);

if (Number.isSafeInteger(number)) {
return _options.alwaysParseAsBig
? (_options.useNativeBigInt ? BigInt(number) : new BigNumber(number))
: number;
}

// If the number has a decimal or exponential part, it can't be
// represented as BigInt, not always.
if (/[\.eE]/.test(string)) {
if (_options.storeAsString) {
const exponent = Math.floor(Math.log2(Math.abs(number)));
if (exponent > 53) {
return string;
}
}
return number;
}

// Remaining cases: large integers
return _options.storeAsString
? string
: (_options.useNativeBigInt ? BigInt(string) : new BigNumber(string));
}
},
string = function () {
Expand Down
22 changes: 22 additions & 0 deletions test/bigint-parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,26 @@ describe("Testing native BigInt support: parse", function () {
expect(output).to.equal(input);
done();
});

it("Should show JSONbig parses decimal numbers as numbers, not strings", function (done) {
var JSONbig = require('../index')({
"storeAsString": true
});
var input = '{"testNumber": 95.45454545454545}';
var obj = JSONbig.parse(input);
expect(obj.testNumber.toString(), "test number").to.equal("95.45454545454545");
expect(typeof obj.testNumber, "test number").to.equal('number');
done();
});

it("Should show JSONbig parses large decimal numbers as strings when storeAsString is true", function (done) {
var JSONbig = require('../index')({
"storeAsString": true
});
var input = '{"largeDecimal": 100000000000000000001.23}';
var obj = JSONbig.parse(input);
expect(obj.largeDecimal, "large decimal number").to.equal("100000000000000000001.23");
expect(typeof obj.largeDecimal, "large decimal number").to.equal('string');
done();
});
});