Skip to content
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
7 changes: 5 additions & 2 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,12 @@ var json_parse = function (options) {
// Parse a number value.

var number,
string = '';
string = '',
is_positive = true;

if (ch === '-') {
string = '-';
is_positive = false;
next('-');
}
while (ch >= '0' && ch <= '9') {
Expand All @@ -202,8 +204,9 @@ var json_parse = function (options) {
}
}
number = +string;
if (isNaN(number)) error(`Bad number`);
if (!isFinite(number)) {
error('Bad number');
return is_positive ? Infinity : -Infinity;
} else {
if (BigNumber == null) BigNumber = require('bignumber.js');
if (Number.isSafeInteger(number))
Expand Down
23 changes: 22 additions & 1 deletion test/bigint-parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe("Testing native BigInt support: parse", function () {
return;
}
var input = '{"big":92233720368547758070,"small":123,"deci":1234567890.0123456,"shortExp":1.79e+308,"longExp":1.7976931348623157e+308}';
var infInput = '{"upperInf":1.797693134862316e+308,"lowerInf":-1.797693134862316e+308}';

it("Should show JSONbig does support parsing native BigInt", function (done) {
var JSONbig = require('../index')({
Expand Down Expand Up @@ -71,4 +72,24 @@ describe("Testing native BigInt support: parse", function () {
expect(output).to.equal(input);
done();
});
});

it(`Should show JSONbig parses a number bigger than the upper infinity limit (> 1.797693134862315E+308) as Infinity`, function (done) {
var JSONbig = require('../index')({
"alwaysParseAsBig": true,
"useNativeBigInt": true
});
var obj = JSONbig.parse(infInput);
expect(obj.upperInf, "upper infinity").to.equal(Infinity);
done();
});

it(`Should show JSONbig parses a number smaller than the lower infinity limit (< -1.797693134862315E+308) as -Infinity`, function (done) {
var JSONbig = require('../index')({
"alwaysParseAsBig": true,
"useNativeBigInt": true
});
var obj = JSONbig.parse(infInput);
expect(obj.lowerInf, "lower infinity").to.equal(-Infinity);
done();
});
});