-
Notifications
You must be signed in to change notification settings - Fork 572
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
Floats are serialized with unnecessary decimal place #562
Comments
On the other hand, it does provide a hint to languages without implicit integer→float conversion that the value should be deserialized as a float, not an integer. Whether that's useful in a real-world context or a footgun is anybody's guess though. (The question is how likely such a program is to encounter a whole number during testing rather than production.) |
Perhaps that is useful. Maybe there could be an option to choose how the JSON gets formatted? |
This is intentional, but can be customized by providing a Formatter impl with the behavior you want. |
For anyone with this problem but only want to change the behavior of one struct field, the following may be a good alternative to writing your own formatter: #[derive(Serialize)]
struct Example {
#[serde(serialize_with = "custom_func")]
x: f32
} |
I guess my bigger question to this, is why is this intentional? This isn't common with web based applications. I'm running into an issue right now where I have a rust application talking to a web application and they're trying to compare their JSON payloads byte wise, and this is causing indifferences. |
I have a question that How to serialize an |
What's even more concerning is that my struct with |
With this struct definition:
Instantiated like this:
serde_json
serializes the struct like this:But I'd expect the serialized output to be:
Even though the JSON that serde produces is technically valid, it's different from how most other languages serialize floats, and it's even different from how rust's built-in formatter formats floats:
format!("{}", 1.0f64)
returns1
.The text was updated successfully, but these errors were encountered: