sidorares / json-bigint

JSON.parse/stringify with bigints support
MIT License
790 stars 189 forks source link

`BigInt` wrapper used with decimals #73

Open nicholaschiang opened 2 years ago

nicholaschiang commented 2 years ago

When the useNativeBigInt option is enabled, this package attempts to wrap decimals (e.g. 0.990011990070343) in the native BigInt wrapper which fails (as they're obviously not integers). Instead, json-bigint should be smart enough to realize that a long floating point number is not an integer and thus should not be wrapped at all.

nicholaschiang commented 2 years ago

Here's the error I'm encountering:

[dev:remix] SyntaxError: Cannot convert 0.990011990070343 to a BigInt
[dev:remix]     at BigInt (<anonymous>)
[dev:remix]     at number (/home/nchiang/repos/tweetscape/node_modules/json-bigint/lib/parse.js:215:15)
[dev:remix]     at value (/home/nchiang/repos/tweetscape/node_modules/json-bigint/lib/parse.js:397:41)
[dev:remix]     at object (/home/nchiang/repos/tweetscape/node_modules/json-bigint/lib/parse.js:367:27)
[dev:remix]     at value (/home/nchiang/repos/tweetscape/node_modules/json-bigint/lib/parse.js:389:16)
[dev:remix]     at array (/home/nchiang/repos/tweetscape/node_modules/json-bigint/lib/parse.js:314:22)
[dev:remix]     at value (/home/nchiang/repos/tweetscape/node_modules/json-bigint/lib/parse.js:391:16)
[dev:remix]     at /home/nchiang/repos/tweetscape/node_modules/json-bigint/lib/parse.js:410:14
[dev:remix]     at Array.parseJson (/home/nchiang/repos/tweetscape/app/db.server.ts:53:16)
[dev:remix]     at Result.parseRow (/home/nchiang/repos/tweetscape/node_modules/pg-promise/node_modules/pg/lib/result.js:68:38)
[dev:remix]     at Query.handleDataRow (/home/nchiang/repos/tweetscape/node_modules/pg-promise/node_modules/pg/lib/query.js:87:26)
[dev:remix]     at Client._handleDataRow (/home/nchiang/repos/tweetscape/node_modules/pg-promise/node_modules/pg/lib/client.js:345:22)
[dev:remix]     at Connection.emit (node:events:520:28)
[dev:remix]     at /home/nchiang/repos/tweetscape/node_modules/pg-promise/node_modules/pg/lib/connection.js:114:12
[dev:remix]     at Parser.parse (/home/nchiang/repos/tweetscape/node_modules/pg-protocol/src/parser.ts:104:9)
[dev:remix]     at Socket.<anonymous> (/home/nchiang/repos/tweetscape/node_modules/pg-protocol/src/index.ts:7:48)
sidorares commented 2 years ago

can you provide simple repro example?

nicholaschiang commented 2 years ago

@sidorares I suspect this is actually already fixed in master:

          // Number with fractional part should be treated as number(double) including big integers in scientific notation, i.e 1.79e+308
          return _options.storeAsString
            ? string
            : /[\.eE]/.test(string)
            ? number
            : _options.useNativeBigInt
            ? BigInt(string)
            : new BigNumber(string);

It just hasn't been released to NPM yet (see #74) and thus I'm seeing this bug in production.