-
Notifications
You must be signed in to change notification settings - Fork 196
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
Bug; parse tries to parse float numbers as BigInt
.
#49
Comments
To reproduce, this is a simple program:
This is where the bug is Line 215 in 390482a
|
Could the fix for this be as simple as looking for a |
Just came across this today, and totally agree with @airhorns that this is easily triggered by the users rather than the developers. |
My solution is removing useNativeBigInt options. I've read the readme quite a lot of times. Seems that it is trying to say floating number should consider as bigint. However, native bigint does not support floating number. Anyway, this should be fixed instead of throwing an error to user. 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...... |
There seems to be a fix already in master for this, only the package hasn't seen an official npm release since somewhere in 2020. At least for me it works now when I use the latest version from github, as explained in this issue For example for yarn I ran: yarn remove json-bigint And then the version from gh:
|
where it tries to parse float numbers as BigInt
Fix for json-bigint bug sidorares/json-bigint#49 where it tries to parse float numbers as BigInt
For anyone who wants to use only the native import 'core-js/modules/esnext.json.parse';
import 'core-js/modules/esnext.json.raw-json';
export const ogirinalJSONParse = JSON.parse;
export interface JSONReviverContext {
source: string;
}
const INTEGER_REGEX = /^-?\d+$/;
function isInteger(value: string) {
return INTEGER_REGEX.test(value);
}
/**
* Parse a JSON string with potential BigInt values.
*/
const parse: typeof ogirinalJSONParse = (text, reviver) => {
return ogirinalJSONParse(
text,
// cannot use arrow function because we want to keep `this` context
function reviveWithBigInt(key, value, context?: JSONReviverContext) {
const obj = this;
// @ts-expect-error Expected 3 arguments, but got 4.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const finalize = (val: any) => (reviver ? reviver.call(obj, key, val, context) : val);
if (
context?.source &&
typeof value === 'number' &&
typeof context?.source === 'string' &&
(value < Number.MIN_SAFE_INTEGER || value > Number.MAX_SAFE_INTEGER) &&
isInteger(context.source) &&
BigInt(value) !== BigInt(context.source)
) {
return finalize(BigInt(context.source));
}
return finalize(value);
},
);
};
// Patch the default JSON.parse to support BigInt values.
JSON.parse = parse;
// This PATCH makes `JSON.stringify` support BigInt values.
// @ts-expect-error Property 'toJSON' does not exist on type 'BigInt'.ts(2339)
// eslint-disable-next-line no-extend-native
BigInt.prototype.toJSON = function toJSON() {
// @ts-expect-error Property 'rawJSON' does not exist on type 'JSON'.
return JSON.rawJSON(this.toString());
};
// Named export makes auto-imports in IDE easier.
const JSONBigInt = {
parse,
stringify: JSON.stringify,
};
export default JSONBigInt; |
Thanks @vazkir for the solution, also linking to the other issue. I did something similar with npm. |
I also encountered this problem. Notably, it happens even for numbers that comfortably fit in IEE754 double, like this one:
But would work fine with native JSON:
|
Parse tries to convert float number as BigInt. It is a bug because it should not try to cast Float numbers to BigInt
SyntaxError: Cannot convert 0.8120414029545044 to a BigInt
The text was updated successfully, but these errors were encountered: