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: support unmarshaling empty string into float #47102

Closed
theWayToAI opened this issue Jul 9, 2021 · 2 comments
Closed

encoding/json: support unmarshaling empty string into float #47102

theWayToAI opened this issue Jul 9, 2021 · 2 comments
Labels
FeatureRequest FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@theWayToAI
Copy link

theWayToAI commented Jul 9, 2021

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

$ go version
1.16.4

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

mac M1

go env Output
$ go env

What did you do?

        jsonData := `{"price":"","k":"ws"}`
	var req struct {
		Price     int64   `json:"price,omitempty,string"`
		K      string  `json:"k"`
	}
	err := json.Unmarshal([]byte(jsonData), &req)
	fmt.Println(err)
	fmt.Printf("%+v", req)

What did you expect to see?

Hope that err can be nil.

Hope that encoding/json can add a new tag like omitempty to ignore the parse err when the input string is "".
This issue has mentioned: #26800
In this issue, "" parse to 0 is not good, I agree that.
But hope that can add a new way for us to ignore this kinds of err instead of throwing an error.
Because we can ofter receive the json string like "price":"", it's right format, but it's inconvenient for us to parse it when there are a lot of fields like this, now we have to define all the fields as string, and parse it one by one using strconv.ParseFloat(), and judge every error and ignore the string "" ...
Thanks.

What did you see instead?

err: json: invalid use of ,string struct tag, trying to unmarshal "" into int64

@mvdan
Copy link
Member

mvdan commented Jul 9, 2021

You can have custom behavior via UnmarshalJSON methods; I don't see why we should add more options for more edge cases.

@mknyszek mknyszek changed the title Hope can add a new way to ignore the error of "encoding/json: invalid use of ,string struct tag, trying to unmarshal "" into int or float" encoding/json: support unmarshaling empty string into float Jul 9, 2021
@mknyszek mknyszek added FeatureRequest NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Jul 9, 2021
@mknyszek mknyszek added this to the Backlog milestone Jul 9, 2021
@mknyszek
Copy link
Contributor

mknyszek commented Jul 9, 2021

I think I have to agree with @mvdan. I believe this behavior is already achievable with a UnmarshalJSON method, either on the req struct, or by defining a new type for your Price field (e.g. type Price float64). This way you don't need to define the price as a string, just use this type where you need it.

Please comment if we're misunderstanding, but closing for now.

@mknyszek mknyszek closed this as completed Jul 9, 2021
@golang golang locked and limited conversation to collaborators Jul 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

4 participants