5 ms·
Is this ok ? Python 3.13.3 (main, May 21 2025, 07:49:52) [GCC 14.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >
by JackSlateur 1y ago
Is this ok ?
Python 3.13.3 (main, May 21 2025, 07:49:52) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more
information.
>>> import json
>>>
json.loads('47234762761726473624762746721647624764380000000000000000000000000000000000000000000')
47234762761726473624762746721647624764380000000000000000000000000000000000000000000
- jazzyjackson 1y agoyes, python falls into the sane language category with arbitrary-precision arithmetic
- faresahmed 1y agoNot so much, >>> s="1"+"0"*4300 >>> json.loads(s) ... ValueError: Exceeds the limit (4300 digits) for integer string conversion: value has 4301 digits; use sys.set_int_max_str_digits() to increase the limit This was done to prevent DoS attacks 3 years ago and have been backported to at least CPython 3.9 as it was considered a CVE. Relevant discussion: https://news.ycombinator.com/item?id=32753235 https://news.ycombinator.com/item?id=32753235 Your sibling comment suggests using decimal.Decimal which handles parsing >4300 digit numbers (by default).
- lifthrasiir 1y agoThis should be interpreted as a stop-gap measure before a subquadratic algorithm can be adopted. Take a look at _pylong.py in new enough CPython.
- teddyh 1y agoI prefer >> import json, decimal >> j = "47234762761726473624762746721647624764380000000000000000000000000000000000000000000" >> json.loads(j, parse_float=decimal.Decimal, parse_int=decimal.Decimal) Decimal('47234762761726473624762746721647624764380000000000000000000000000000000000000000000') This way you avoid this problem: >> import json >> j = "0.47234762761726473624762746721647624764380000000000000000000000000000000000000000000" >> json.loads(j) 0.47234762761726473 And instead can get: >> import json, decimal >> j = "0.47234762761726473624762746721647624764380000000000000000000000000000000000000000000" >> json.loads(j, parse_float=decimal.Decimal, parse_int=decimal.Decimal) Decimal('0.47234762761726473624762746721647624764380000000000000000000000000000000000000000000')
- sevensor 1y agoJust cross your fingers and hope for the best if your data is at any point decoded by a json library that doesn’t support bigints? Python’s ability to handle them is beside the point of they get mangled into ieee754 doubles along the way.