ijl / orjson

Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy
Apache License 2.0
6.29k stars 215 forks source link

Error loading large integer numbers #517

Closed mighabana closed 1 month ago

mighabana commented 2 months ago

Hi, I just came across this error when loading large integers where the integer extracted using orjson does not match the original large integer. Interestingly, the issue does not occur when loading the json file using json.

Here's the sample json file that I'm reading at "file.json":

{
    "locations": [
        {
            "latitudeE7": 351324213,
            "longitudeE7": -1122434441,
            "accuracy": 10,
            "deviceTag": -80241446968629135069,
            "deviceDesignation": "PRIMARY",
            "timestamp": "2017-12-10T23:14:58.030Z"
        }
    ]
}

This is the python code that I ran to compare the output between orjson and json. To highlight the issue, it occurs in the last 4 digits of the device tag that does not match the values in the original json file.

from pathlib import Path
import json
import orjson

fp = Path('.') / "file.json"

ojson_data = orjson.loads(fp.read_text())

ojson_deviceTag = ojson_data.get('locations')[0].get('deviceTag')
print(ojson_deviceTag ) # -8.024144696862913e+19

json_data = json.loads(fp.read_text())
json_deviceTag = json_data.get('locations')[0].get('deviceTag')
print(json_deviceTag) # -80241446968629135069

# The issue is more obvious when casting to int
print(int(ojson_deviceTag)) # -80241446968629133312