-
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
proposal: encoding/json: Unmarshal string to number #71545
Comments
Related Issues (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
the |
Hi @erikpa1, thanks for the issue. It seems to me that the "json" package already supports this with the type Foo struct {
Speed int64 `json:"speed,string"`
}
var foo Foo
json.Unmarshal([]byte(`{"speed":"9000"}`), &foo)
fmt.Println(foo) Regarding the use of stringified numbers being "weird", this is largely an artifact of JSON having JavaScript as its heritage, where numbers in JavaScript are float64. Thus, many JSON parsers will mangle 64-bit integers if sufficiently large since they cannot be accurately represented in a float64. The use of strings for wide integers became standard practice to work around this quirk. One could argue that JavaScript is the "weird" one. |
@dsnet Thanks I will try :) |
Proposal Details
Hi, when working with external APIs and so on there area always some weirdos in this world, which sends numbers as a string in json {"data" : "50"} instead of {"data": 50} , this also happens when you you use some default form behavior in client side javascript library.
Could be nice define in
json:"speed, isNumberString"
The text was updated successfully, but these errors were encountered: