-
Notifications
You must be signed in to change notification settings - Fork 212
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
"infer_deserialize" inference causes loss of precision #212
Comments
Seems like a lot of problems stem from this type inference. Which cases does it actually improve? |
For when you need to call I don't really understand this bug report.
|
I think my mental model of the serde You might be right that I shouldn't be calling There are certain serde features that rely on |
Yeah, I think there are two problems working against you here. Firstly, Serde is quite complex and it supports some use cases a lot better than others. Secondly, CSV is a really weak format. It doesn't have any data types. So if the csv crate wants to support the If there's a way to have our cake and eat it too, then great. But to be honest, it's hard for me to make progress on these sorts of problems. Serde and its interaction with the csv crate is so complex that I have to re-contextualize in order to come up with an answer, and re-contextualizing is not cheap. :-/ |
@bchallenor I think it would be better for your type to do a kind of its own |
What version of the
csv
crate are you using?1.1.3
Briefly describe the question, bug or feature request.
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
. This is a problem for types whose serialization looks like that of a float, but in fact is more precise than a float. For these types, it is important that they are deserialized from the originalstr
, without an intermediate conversion to float.Include a complete program demonstrating a problem.
What is the observed behavior of the code above?
What is the expected or desired behavior of the code above?
Assertion succeeds.
Proposed change
I tried patching rust-csv to disable the type inference in
infer_deserialize
:This causes the assertion to succeed. Could this be configurable?
An alternative that does not involve patching rust-csv would be to change
deserializer.deserialize_any(Visitor)
todeserializer.deserialize_str(Visitor)
inBigNum
. While it would fix this case, it would preventBigNum
from being deserialized via another (non-CSV) serde backend that happens to produce integers.infer_deserialize
also causes similar problems wheredeserialize_any
is invoked by Serde itself, rather than by user code. I think this corresponds to cases where Serde needs to do backtracking over the same input. #151 is one such example, and I've run into another with#[serde(untagged)]
(I can open another issue for that if you like). The proposed change would fix those too.Serde says "Untagged and internally tagged enums are only supported in self-describing formats" but I think a legitimate view of CSV is that it is a self-describing format where every type is a string.
The text was updated successfully, but these errors were encountered: