-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
encoding/json: marshaling can drop .0 for floats #63514
Comments
I will try debugging this and following up: go/src/encoding/json/encode.go Line 159 in eb832af
|
Duplicate of #42246 |
i disagree with the decisions there. it's important to maintain the .0's with the json.Marshal method because of the reason that JSON is used very prominently with systems that don't necessarily deal with types the same way. as i demonstrated earlier, this leads to weird outcomes. types are not dealt with similarly everywhere, and it's nice for the library to acknowledge and help with that. what do you think? @seankhliao |
as the comments in the linked issue mention, we've decided to align with how JavaScript handles numbers #42246 (comment) |
Like @seankhliao says, there are ways to precisely control how JSON numbers will be encoding and decoding to JSON, e.g. https://pkg.go.dev/encoding/json#Number or a custom marshaler/unmarshaler. Also note that your original example seems a bit confusing. The float64 value |
hey @mvdan! thanks for the reply. so:
|
What version of Go are you using (
go version
)? 1.19Does this issue reproduce with the latest release? Yes
What did you do?
This bug led to this: opensearch-project/opensearch-go#386
Here is the go.dev/play package: https://go.dev/play/p/lw648QN2w6Y
What did you expect to see?
It is however formatted to be:
{"float_value":5}
This can cause weird type errors that would become hard to find. Let's fix this.
What did you see instead?
Expected the JSON to be formatted to:
{"float_value":5.00}
(since the value at hand is float)I want to solve this issue myself. It looks like an easy fix. Please point me to the right direction so that I can close this myself for the next issue.
The text was updated successfully, but these errors were encountered: