-
Notifications
You must be signed in to change notification settings - Fork 186
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
Loss of precision in serde deserialization due to f64 conversion #279
Comments
Out of curiosity, does enabling the |
Yes, enabling I agree the feature might be more discoverable if it were renamed. Perhaps Interestingly, it breaks another use case I have for rust-csv + rust-decimal involving What do you think about extending the conditional compilation to the methods of
|
For now I've added a PR to try the aliasing approach however would like to be able to reproduce the failure case you mentioned. If you have any example snippets then please feel free to share and I'll add some tests to fiddle around with! |
#279: Alias serde-bincode -> serde-str to remove bincode confusion
I am seeing a loss of precision in serde deserialization that I believe is due to an intermediate
f64
conversion.I'm trying to deserialize from CSV, so I'm using rust-csv as the serde backend. rust-csv's implementation of
deserialize_any
will try to guess the type of the CSV field based on its contents, so if the field looks like a float, it will end up creating anf64
and passing it tovisit_f64
onDecimalVisitor
. At this point, we lose precision, and even for representable numbers, we lose track of the input scale (e.g. trailing zeros).Here is a patch to add a test exhibiting the problem:
This problem is caused by an unfortunate interaction of (otherwise useful) features in the two libraries: firstly, the field type inference in rust-csv, and secondly, the support for
deserialize_any
in rust-decimal. So I'm not sure where this problem would best be solved:Deserialize
forDecimal
to calldeserialize_str
instead ofdeserialize_any
, the above test passes (but the equivalent JSON test fails, because serde_json needs to be able to deserialize int/float values).deserialize_any
to unconditionally invokevisit_str
, rather than trying to guess the type.The former has precedent in that rust-decimal already has the serde-float feature to do exactly this - it's just limited to bincode right now. Enabling this feature for CSV would unfortunately break support for int and float representations in JSON, so an app that uses both CSV and JSON would be forced to use a string representation of
Decimal
in JSON as well.The text was updated successfully, but these errors were encountered: