Skip to content

Fix parsing of very large bigints #80

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

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
7 changes: 6 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ var BigNumber = null;
const suspectProtoRx = /(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])/;
const suspectConstructorRx = /(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)/;

// (c) CC BY-SA 4.0
// https://stackoverflow.com/a/57342060
// Modified to remove support for + prefix
const numberRx = /^\s*-?(\d+|\d*\.\d+|\d+\.\d*)([eE][+-]?\d+)?\s*$/;

/*
json_parse.js
2012-06-20
Expand Down Expand Up @@ -202,7 +207,7 @@ var json_parse = function (options) {
}
}
number = +string;
if (!isFinite(number)) {
if (!numberRx.test(string)) {
error('Bad number');
} else {
if (BigNumber == null) BigNumber = require('bignumber.js');
Expand Down
4 changes: 3 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,"deci":1234567890.0123456,"shortExp":1.79e+308,"longExp":1.7976931348623157e+308}';
var input = `{"huge":1${'0'.repeat(309)},"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 @@ -19,6 +19,8 @@ describe("Testing native BigInt support: parse", function () {
expect(obj.small, "small int").to.equal(123);
expect(obj.big.toString(), "big int").to.equal("92233720368547758070");
expect(typeof obj.big, "big int").to.equal('bigint');
expect(obj.huge.toString(), "huge int").to.equal(`1${'0'.repeat(309)}`);
expect(typeof obj.huge, "huge int").to.equal('bigint');
done();
});

Expand Down