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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,23 @@ Default type: object, With option type: bigint

```

**Exception**: if the number being parsed is a big decimal number, still returns it as BigNumber.

```js
var JSONbig = require('json-bigint');
var JSONbigNative = require('json-bigint')({ useNativeBigInt: true });
var key = '{ "key": 993143214321423154315154321.1 }';
console.log(`\n\nStoring the Number as 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
);
````

#### options.alwaysParseAsBig, boolean, default false

Specifies if all numbers should be stored as BigNumber.
Expand Down
27 changes: 20 additions & 7 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,24 @@ var json_parse = function (options) {
at += 1;
return ch;
},
asBigNumber = function (v) {
if (BigNumber == null) {
BigNumber = require('bignumber.js');
}

var bigNum = new BigNumber(v);
if (!_options.useNativeBigInt) {
return bigNum;
}

if (!bigNum.isInteger()) {
// only case where we return BigNumber when 'useNativeBigInt' is true
// would only happen if parsing a very big decimal number
return bigNum
}

return BigInt(bigNum.toFixed());
},
number = function () {
// Parse a number value.

Expand Down Expand Up @@ -205,21 +223,16 @@ var json_parse = function (options) {
if (!isFinite(number)) {
error('Bad number');
} else {
if (BigNumber == null) BigNumber = require('bignumber.js');
//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);
: asBigNumber(string);
else
return !_options.alwaysParseAsBig
? number
: _options.useNativeBigInt
? BigInt(number)
: new BigNumber(number);
: asBigNumber(number);
}
},
string = function () {
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"devDependencies": {
"chai": "4.2.0",
"fast-check": "^2.6.0",
"mocha": "8.0.1"
}
}
11 changes: 11 additions & 0 deletions test/bigint-parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var mocha = require('mocha')
, assert = require('chai').assert
, expect = require('chai').expect
, BigNumber = require('bignumber.js')
, fc = require('fast-check')
;

describe("Testing native BigInt support: parse", function () {
Expand Down Expand Up @@ -56,4 +57,14 @@ describe("Testing native BigInt support: parse", function () {
expect(output).to.equal(input);
done();
});

it("Should show JSONbig does support native Bigint parse/stringify roundtrip with any valid JSON value", function (done) {
var JSONbig = require('../index')({
"useNativeBigInt": true
});
fc.assert(fc.property(fc.jsonObject(), (v) => {
JSONbig.parse(JSONbig.stringify(v))
}));
done();
});
});
9 changes: 9 additions & 0 deletions test/bigint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var mocha = require('mocha')
, assert = require('chai').assert
, expect = require('chai').expect
, BigNumber = require('bignumber.js')
, fc = require('fast-check')
;

describe("Testing bigint support", function(){
Expand All @@ -28,4 +29,12 @@ describe("Testing bigint support", function(){
expect(output).to.equal(input);
done();
});

it("Should show JSONbig does support bigint parse/stringify roundtrip with any valid JSON value", function (done) {
var JSONbig = require('../index');
fc.assert(fc.property(fc.jsonObject(), (v) => {
JSONbig.parse(JSONbig.stringify(v))
}));
done();
});
});