Skip to content
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

Closed
0x0elliot opened this issue Oct 12, 2023 · 7 comments
Closed

encoding/json: marshaling can drop .0 for floats #63514

0x0elliot opened this issue Oct 12, 2023 · 7 comments

Comments

@0x0elliot
Copy link

0x0elliot commented Oct 12, 2023

What version of Go are you using (go version)? 1.19

$ go version
1.19

Does 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
Screenshot 2023-10-12 at 3 56 00 PM

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.

@0x0elliot 0x0elliot changed the title encoding/json: Json.Marshal doesn't handle types well encoding/json: json.Marshal doesn't handle types well Oct 12, 2023
@0x0elliot
Copy link
Author

I will try debugging this and following up:

func Marshal(v any) ([]byte, error) {

@seankhliao seankhliao changed the title encoding/json: json.Marshal doesn't handle types well encoding/json: marshaling can drop .0 for floats Oct 12, 2023
@seankhliao
Copy link
Member

cc @mvdan @dsnet

@seankhliao
Copy link
Member

Duplicate of #42246

@seankhliao seankhliao marked this as a duplicate of #42246 Oct 12, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Oct 12, 2023
@0x0elliot
Copy link
Author

0x0elliot commented Oct 12, 2023

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

@seankhliao
Copy link
Member

as the comments in the linked issue mention, we've decided to align with how JavaScript handles numbers #42246 (comment)

@mvdan
Copy link
Member

mvdan commented Oct 12, 2023

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 5.00 does not have two trailing zeros "as a string". You can notice that, for example, 5.0 == 5.000 is true, because they are both exactly the same float64 value, and not strings.

@0x0elliot
Copy link
Author

0x0elliot commented Oct 12, 2023

hey @mvdan!

thanks for the reply. so:

  1. don't worry about my confusing example. but i am glad you folks got the point.
  2. you're actually correct. it's better to deal with how js deals with JSON. no matter how i hate how it deals with those things.
Screenshot 2023-10-12 at 6 29 41 PM
  1. Backing up my last point that I made, fuck javascript.

@golang golang locked and limited conversation to collaborators Oct 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants