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

Custom JSON parser to handle bigint #379

Open
SebastienGllmt opened this issue Apr 11, 2025 · 0 comments
Open

Custom JSON parser to handle bigint #379

SebastienGllmt opened this issue Apr 11, 2025 · 0 comments

Comments

@SebastienGllmt
Copy link

Currently, this project relies on JSON.parse to load JSON files. However, JSON.parse is lossy on numbers larger than Number.MAX_SAFE_INTEGER

ex:

console.log(
    JSON.parse(`${Number.MAX_SAFE_INTEGER + 2}`)
);

Correct output: 9007199254740993
What JS actually prints: 9007199254740992

Some important things to note:

  1. You cannot solve this by just passing a custom resolver to JSON.parse. Unfortunately, you need to replace JSON.parse entirely to be able to resolve large numbers without precision loss
  2. There is no standard for how this case is handled. Should a library convert only numbers that exceed a certain value as bigint? That would cause the type of everything to become number | bigint everywhere which people may not want. In a lot of cases, the way to handle this ends up being context-specific

Given this, it may be best to just have an agreed-upon way to allow a user-provided JSON parser to use when resolving references

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant