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

Some big numbers not converted to string #44

Open
djgrant opened this issue Feb 10, 2021 · 0 comments
Open

Some big numbers not converted to string #44

djgrant opened this issue Feb 10, 2021 · 0 comments

Comments

@djgrant
Copy link

djgrant commented Feb 10, 2021

Some numbers which are larger than Number.MAX_SAFE_INTEGER can still be represented accurately as a regular JavaScript number.

In those cases jsonparse will return them as a number, rather than a string.

I'm not sure if that's intentional but I thought it was worth flagging. I was expecting all numbers outside double precision range to be returned as strings. Here's a few examples where this is not happening:

144380449412828603 string 
144122580203659657 string
144250504882249760 number
144222334382612875 string
144353568153548541 string
144131338871386780 number
144274369105917272 string
144188125506805060 number

One potential issue that might arise from this is passing the output from jsonparse to BigInt:

Number('144188125506805060');         // 144188125506805060  👍 
BigInt('144188125506805060');         // 144188125506805060n 👍  
BigInt(Number('144188125506805060')); // 144188125506805056n 👎 

The cause of the issue (if it is indeed considered an issue) is this condition:

if ((text.match(/[0-9]+/) == text) && (result.toString() != text)) {

An additional check against Number.MAX_SAFE_INTEGER could suffice as a solution, though may not be backwards compatible.

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