Nykakin / chompjs

Parsing JavaScript objects into Python data structures
MIT License
197 stars 11 forks source link

Add leading zero to some floats #10

Closed Nykakin closed 4 years ago

Nykakin commented 4 years ago

json.loads cannot handle floats in range (-1, 1) if they don't have leading zero:

>>> import json
>>> json.loads('{"Test": 0.99}')
{u'Test': 0.99}
>>> json.loads('{"Test": .99}')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

Therefore the parser needs to add this 0 itself.