Skip to content

Some big numbers not converted to string #44

@djgrant

Description

@djgrant

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions