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

#25 parse very big numeric values #26

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
adc33f1
use function Bignumber.isFinite() for checking if a numeric value is …
Nov 20, 2018
0c4abf2
check length of numbers with bigNumberValue.toFixed().length instead …
Nov 20, 2018
7054c55
add test case for checking if numeric values larger than 1.7976931348…
Nov 20, 2018
d5cf33b
chore(CI): use node 6 for building this module with travis-ci
SebastianG77 Dec 2, 2018
3e84008
chore(package.json): update package.json
SebastianG77 Dec 2, 2018
352b0b5
docs(README.md): adjust README.md to changes
SebastianG77 Dec 2, 2018
3c3a3c7
docs(README.md): fix example in README.md
SebastianG77 Dec 2, 2018
aa65a35
docs(README.md): fix and improve examples in README.md
SebastianG77 Dec 2, 2018
5f1f42f
docs(README.md): add clarification regarding parsing big numbers in s…
SebastianG77 Dec 2, 2018
05e3022
docs(README.md): adjust structure of sentence
SebastianG77 Dec 2, 2018
f11021d
docs(README.md): change sentence
SebastianG77 Dec 2, 2018
f4baea1
docs(README.md): adjust example in README.md
SebastianG77 Dec 2, 2018
ab19c8b
chore(package.json): update version 0.4.0 -> 0.4.1
SebastianG77 Dec 2, 2018
b81f2f0
description(package.json): change description in package.json to summ…
SebastianG77 Dec 2, 2018
66c349f
chore(package.json): adjust version: 0.4.1 -> 0.4.2
SebastianG77 Dec 2, 2018
bd978fa
docs(README.md): fix link to travis ci badge
SebastianG77 Dec 3, 2018
1454190
chore(package.json): update version 0.4.2 -> 0.4.3
SebastianG77 Dec 3, 2018
a9d0c2a
docs(README.md): fix typo in README.md
Dec 4, 2018
7ca3505
chore(package.json): update version: 0.4.3 -> 0.4.4
Dec 4, 2018
31c7ee4
fix(dependencies): update dependencies and adjust test case due to br…
SebastianG77 Jul 20, 2019
67e95dd
tests(package.json): adjust test script such that it now also works l…
SebastianG77 Jul 20, 2019
b544514
refactor(tests): remove unused imports in test files
SebastianG77 Jul 20, 2019
6a9d328
chore(package.json): update version: 0.4.4 -> 0.4.5
SebastianG77 Jul 20, 2019
2962df1
feat(index.d.ts): add typescript typings
xamgore Oct 14, 2019
665a228
docs(README.md): enable code hightlighting
xamgore Oct 14, 2019
eea4103
Merge pull request #1 from xamgore/master
SebastianG77 Oct 26, 2019
a3ecd0a
Merge pull request #2 from xamgore/patch-1
SebastianG77 Oct 26, 2019
c55da5e
docs(README.md): move comment containing expected output to new line …
SebastianG77 Oct 26, 2019
4c0e7af
chore(package.json): update version 0.4.5 -> 0.4.6
SebastianG77 Oct 26, 2019
bc02015
chore(package.json): update dependency of mocha ~6.2.0 -> ~7.1.1 and …
SebastianG77 Apr 10, 2020
4947f0e
chore(package.json): update version 0.4.6 -> 0.4.7
SebastianG77 Apr 10, 2020
e40189c
chore(travis.yml): update nodeJS version for travis-ci to 8, because …
SebastianG77 Apr 10, 2020
07ffe44
* chore(package.json): update dependency of mocha ~7.1.1 -> ~8.1.1 an…
SebastianG77 Aug 17, 2020
1c1a9f3
chore(package.json): update version 0.4.7 -> 0.4.8
SebastianG77 Aug 17, 2020
e7567de
* chore(travis.yml): update nodeJS version for travis-ci to 10, becau…
SebastianG77 Aug 17, 2020
6dab329
Merge branch 'master' of https://github.com/sidorares/json-bigint
SebastianG77 Sep 1, 2020
06118ba
chore(yarn.lock): remove yarn.lock as json-bigint uses npm for buildi…
SebastianG77 Sep 1, 2020
b9bdcd6
chore(package-lock.json): update package-lock.json
SebastianG77 Sep 1, 2020
43190a0
remove redundant call of require('bignumber.js')
SebastianG77 Dec 10, 2020
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
47 changes: 47 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export declare type JSONBig = {
/**
* Converts a JavaScript Object Notation (JSON) string into an object.
* @param text A valid JSON string.
* @param reviver A function that transforms the results. This function is called for each member of the object.
* If a member contains nested objects, the nested objects are transformed before the parent object is.
*/
parse(text: string, reviver?: (this: any, key: string, value: any) => any): any;

/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
* @param value A JavaScript value, usually an object or array, to be converted.
* @param replacer A function that transforms the results.
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*/
stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;

/**
* Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
* @param value A JavaScript value, usually an object or array, to be converted.
* @param replacer An array of strings and numbers that acts as a approved list for selecting the object properties that will be stringified.
* @param space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*/
stringify(value: any, replacer?: (number | string)[] | null, space?: string | number): string;
}


export interface JSONBigParseOptions {
/**
* Not being strict means do not generate syntax errors for "duplicate key".
* @default false
*/
strict?: boolean;

/**
* Whether the values should be stored as BigNumber (default) or a string
* @default false
*/
storeAsString?: boolean;
}


declare const JSONbig: JSONBig & {
(options: JSONBigParseOptions): JSONBig;
}

export default JSONbig
16 changes: 10 additions & 6 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,24 +202,28 @@ var json_parse = function (options) {
}
}
number = +string;
if (!isFinite(number)) {
error('Bad number');

if (BigNumber == null)
BigNumber = require('bignumber.js');
var bigNumberValue = new BigNumber(string);
if (!bigNumberValue.isFinite()) {
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)

if (bigNumberValue.toFixed().length > 15)
return _options.storeAsString
? string
: _options.useNativeBigInt
? BigInt(string)
: new BigNumber(string);
: bigNumberValue;
else
return !_options.alwaysParseAsBig
? number
: _options.useNativeBigInt
? BigInt(number)
: new BigNumber(number);
: bigNumberValue;
}
},
string = function () {
Expand Down
157 changes: 134 additions & 23 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lib/stringify.js"
],
"scripts": {
"test": "./node_modules/mocha/bin/mocha -R spec --check-leaks test/*-test.js"
"test": "mocha -R spec --check-leaks test/*-test.js"
},
"repository": {
"type": "git",
Expand All @@ -29,6 +29,6 @@
},
"devDependencies": {
"chai": "4.2.0",
"mocha": "8.0.1"
"mocha": "8.1.1"
}
}
20 changes: 15 additions & 5 deletions test/bigint-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
var mocha = require('mocha')
, assert = require('chai').assert
, expect = require('chai').expect
, BigNumber = require('bignumber.js')
;
var expect = require('chai').expect,
BigNumber = require('bignumber.js');

describe("Testing bigint support", function(){
var input = '{"big":9223372036854775807,"small":123}';
Expand All @@ -28,4 +25,17 @@ describe("Testing bigint support", function(){
expect(output).to.equal(input);
done();
});

it("Should be able to parse numeric values larger than 1.7976931348623157e+308", function(done){
var JSONbig = require('../index');
var veryBigInput = '{"big":3e+500}';

var obj = JSONbig.parse(veryBigInput);
expect(obj.big.toString(), "string from big int").to.equal("3e+500");
expect(obj.big, "instanceof big int").to.be.instanceof(BigNumber);

var output = JSONbig.stringify(obj);
expect(output).to.equal(veryBigInput);
done();
});
});
Loading