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

proposal: encoding/json: Unmarshal string to number #71545

Closed
erikpa1 opened this issue Feb 3, 2025 · 5 comments
Closed

proposal: encoding/json: Unmarshal string to number #71545

erikpa1 opened this issue Feb 3, 2025 · 5 comments
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Milestone

Comments

@erikpa1
Copy link

erikpa1 commented Feb 3, 2025

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"

@gopherbot gopherbot added this to the Proposal milestone Feb 3, 2025
@ianlancetaylor ianlancetaylor changed the title proposal: json: Unmarshal string to number proposal: encoding/json: Unmarshal string to number Feb 3, 2025
@ianlancetaylor
Copy link
Member

CC @dsnet @mvdan

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Feb 3, 2025
@gabyhelp
Copy link

gabyhelp commented Feb 3, 2025

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@gabyhelp gabyhelp added the LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool label Feb 3, 2025
@seankhliao
Copy link
Member

the string json tag option already works.
https://go.dev/play/p/30p8aClQe9S

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 3, 2025
@dsnet
Copy link
Member

dsnet commented Feb 3, 2025

Hi @erikpa1, thanks for the issue.

It seems to me that the "json" package already supports this with the ,string option?
For example:

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.

@erikpa1
Copy link
Author

erikpa1 commented Feb 4, 2025

@dsnet Thanks I will try :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Projects
None yet
Development

No branches or pull requests

7 participants
@ianlancetaylor @dsnet @gopherbot @seankhliao @erikpa1 @gabyhelp and others