isagalaev / ijson

Iterative JSON parser with Pythonic interface
http://pypi.python.org/pypi/ijson/
Other
615 stars 134 forks source link

Floaty numbers are parsed as ints #53

Closed akaIDIOT closed 8 years ago

akaIDIOT commented 8 years ago

common.py makes sure to check whether the decimal 1.0 == int(1.0), return 1 if that is the case. Values sent as 1.0 over the wire show up as 1 in Python. I get that this might in some cases be considered a feature, and JSON makes no explicit distinction between integers and floats, but this is causing issues for round trip (de)serializations.

For reference, Python's default json.loads() does parse ["1.0", 1.0, 1] as-is, where ijson parses it as ["1.0", 1, 1]. Should this behaviour not be either configurable or compatible with the default lib?

akaIDIOT commented 8 years ago
from ijson import common
common.number = lambda value: int(value) if value.isdigit() else float(value)

Works but makes my stomach churn...

isagalaev commented 8 years ago

Yes, it makes sense. Thanks for pointing this out!