🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

json-bigint

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-bigint - npm Package Compare versions

Comparing version

to
0.4.0

19

lib/parse.js

@@ -79,3 +79,5 @@ var BigNumber = null;

"strict": false, // not being strict means do not generate syntax errors for "duplicate key"
"storeAsString": false // toggles whether the values should be stored as BigNumber (default) or a string
"storeAsString": false, // toggles whether the values should be stored as BigNumber (default) or a string
"alwaysParseAsBig": false, // toggles whether all numbers should be Big
"useNativeBigInt": false // toggles whether to use native BigInt instead of bignumber.js
};

@@ -92,2 +94,4 @@

}
_options.alwaysParseAsBig = options.alwaysParseAsBig === true ? options.alwaysParseAsBig : false
_options.useNativeBigInt = options.useNativeBigInt === true ? options.useNativeBigInt : false
}

@@ -179,4 +183,5 @@

if (string.length > 15)
return (_options.storeAsString === true) ? string : new BigNumber(string);
return number;
return _options.storeAsString ? string : _options.useNativeBigInt ? BigInt(string) : new BigNumber(string);
else
return !_options.alwaysParseAsBig ? number : _options.useNativeBigInt ? BigInt(number) : new BigNumber(number);
}

@@ -197,4 +202,7 @@ },

if (ch === '"') {
var startAt = at;
while (next()) {
if (ch === '"') {
if (at - 1 > startAt)
string += text.substring(startAt, at - 1);
next();

@@ -204,2 +212,4 @@ return string;

if (ch === '\\') {
if (at - 1 > startAt)
string += text.substring(startAt, at - 1);
next();

@@ -221,4 +231,3 @@ if (ch === 'u') {

}
} else {
string += ch;
startAt = at;
}

@@ -225,0 +234,0 @@ }

@@ -252,2 +252,3 @@ var BigNumber = require('bignumber.js');

case 'null':
case 'bigint':

@@ -254,0 +255,0 @@ // If the value is a boolean or null, convert it to a string. Note:

{
"name": "json-bigint",
"version": "0.3.1",
"version": "0.4.0",
"description": "JSON.parse with bigints support",
"main": "index.js",
"files": [
"index.js",
"lib/parse.js",
"lib/stringify.js"
],
"scripts": {

@@ -26,5 +31,5 @@ "test": "./node_modules/mocha/bin/mocha -R spec --check-leaks test/*-test.js"

"devDependencies": {
"chai": "~1.9.1",
"mocha": "~1.20.1"
"chai": "4.2.0",
"mocha": "8.0.1"
}
}

@@ -7,4 +7,6 @@ json-bigint

JSON.parse/stringify with bigints support. Based on Douglas Crockford [JSON.js](https://github.com/douglascrockford/JSON-js) package and [bignumber.js](https://github.com/MikeMcl/bignumber.js) library.
JSON.parse/stringify with bigints support. Based on Douglas Crockford [JSON.js](https://github.com/douglascrockford/JSON-js) package and [bignumber.js](https://github.com/MikeMcl/bignumber.js) library.
Native `Bigint` was added to JS recently, so we added an option to leverage it instead of `bignumber.js`. However, the parsing with native `BigInt` is kept an option for backward compability.
While most JSON parsers assume numeric values have same precision restrictions as IEEE 754 double, JSON specification _does not_ say anything about number precision. Any floating point number in decimal (optionally scientific) notation is valid JSON value. It's a good idea to serialize values which might fall out of IEEE 754 integer precision as strings in your JSON api, but `{ "value" : 9223372036854775807}`, for example, is still a valid RFC4627 JSON string, and in most JS runtimes the result of `JSON.parse` is this object: `{ value: 9223372036854776000 }`

@@ -111,3 +113,58 @@

#### options.useNativeBigInt, boolean, default false
Specifies if parser uses native BigInt instead of bignumber.js
example:
```js
var JSONbig = require('json-bigint');
var JSONbigNative = require('json-bigint')({"useNativeBigInt": true});
var key = '{ "key": 993143214321423154315154321 }';
console.log(`\n\nStoring the Number as native BigInt, instead of 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);
```
Output
```
Storing the Number as native BigInt, instead of a BigNumber
Input: { "key": 993143214321423154315154321 }
Default type: object, With option type: bigint
```
#### options.alwaysParseAsBig, boolean, default false
Specifies if all numbers should be stored as BigNumber.
Note that this is a dangerous behavior as it breaks the default functionality of being able to convert back-and-forth without data type changes (as this will convert all Number to be-and-stay BigNumber)
example:
```js
var JSONbig = require('json-bigint');
var JSONbigAlways = require('json-bigint')({"alwaysParseAsBig": true});
var key = '{ "key": 123 }'; // there is no need for BigNumber by default, but we're forcing it
console.log(`\n\nStoring the Number as a BigNumber, instead of a Number`);
console.log('Input:', key);
var normal = JSONbig.parse(key);
var always = JSONbigAlways.parse(key);
console.log('Default type: %s, With option type: %s', typeof normal.key, typeof always.key);
```
Output
```
Storing the Number as a BigNumber, instead of a Number
Input: { "key": 123 }
Default type: number, With option type: object
```
If you want to force all numbers to be parsed as native `BigInt`
(you probably do! Otherwise any calulations become a real headache):
```js
var JSONbig = require('json-bigint')({"alwaysParseAsBig": true, "useNativeBigInt": true});
```
### Links:

@@ -120,1 +177,18 @@ - [RFC4627: The application/json Media Type for JavaScript Object Notation (JSON)](http://www.ietf.org/rfc/rfc4627.txt)

### Note on native BigInt support
#### Stringifying
Full support out-of-the-box, stringifies BigInts as pure numbers (no quotes, no `n`)
#### Limitations
- Roundtrip operations
`s === JSONbig.stringify(JSONbig.parse(s))` but
`o !== JSONbig.parse(JSONbig.stringify(o))`
when `o` has a value with something like `123n`.
`JSONbig` stringify `123n` as `123`, which becomes `number` (aka `123` not `123n`) by default when being reparsed.
There is currently no consistent way to deal with this issue, so we decided to leave it, handling this specific case is then up to users.