-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect parsing of JSON numbers in v0.4 (Regression) #113
Comments
A solution of this is being discussed in #117. But in general I think JSON specifies that numbers are f64, so using those as storage is not considered safe, and should probably be strings. For example, in Python: import json, decimal
j = json.loads("""
{"splits": [
{ "share": 16.7 },
{ "share": 16.7 },
{ "share": 16.7 }
]}
""")
shares = [decimal.Decimal(split['share']) for split in j['splits']]
sum(shares) # Decimal('50.09999999999999786837179272') and more to the point:
But with strings, keeping the digits explicit: j = json.loads("""
{"splits": [
{ "share": "16.7" },
{ "share": "16.7" },
{ "share": "16.7" }
]}
""")
sum(Decimal(split['share']) for split in j['splits']) # Decimal('50.1') But I understand: if you trust the text of JSON anyways, the standards really don't matter and you want to deserialize to the objects you expect. Are you parsing with serde into your own rust structs? Because if I understand correctly, issue #117 would allow you mark your fields with a custom parsing function that parses the JSON fields directly. |
That's not the case, the JSON spec says that any sequence of digits is equally valid:
So it's no different than if you were to intermediately parse as |
In a JSON file, I have numbers that add up to exactly 50:
Thankfully, when parsing them I verify that they add up.
In v0.3 these numbers were correctly parsed as
16.7
, but in v0.4.1 they parse to16.699999999999999289457264239899814128875732421875
.The text was updated successfully, but these errors were encountered: