-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/json: error when unmarshaling empty string to int #22182
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
Comments
The JSON decoder has acted this way since https://golang.org/cl/6035050 in 2012. I don't know what the correct behavior is for unmarshaling an empty string into an int field. |
OK. I have defined a new type to implement my behavior. I just think the behavior now is strange, anyway for the object to receive result will return ZERO value, why doesn't it return 0 directly. |
To the extent that there is a JSON specification, we should follow it. However, I do not know what the JSON specification says for an empty string. |
json.org says "A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes." so "" is valid go just has trouble translating it gracefully. |
It's a valid JSON value, sure, but I think it should probably be an error to unmarshal any string into an int. I'm not sure the specification covers how values should be deserialized into native code. JSON is so weakly typed and unexpected type conversion errors are a cause of so many problems I think it's better to try to enforce type errors when we can. Better to error when a type is confusing than to silently try to convert it, I think. I'm open to changing my mind if you can demonstrate the existing Go behavior allows When you say |
I think we can extend behavior of go-JSON-package, as annotation is supported, why don't we add one more, e.g: default value for different types conversion |
I agree with @kevinburke. I don't think this is a bug or something that should be changed. As a Go developer, who consumes JSON, if I define my field as an Edit: I wanted to elaborate further. I think if you want to have behavior like this inside of Go, you should go down the path @xtudouh went and implement your own type. That allows you to have complete control over what is valid, or an error, without the Go standard library needing to make potentially unsafe assumptions. |
But Go already allows this. That's what this means: type Demo struct { Parse a string as an Integer, erroring if the string does not contain a valid Integer representation.
And it does. In this example it requires the JSON value to be a string, AND it requires the string to contain a valid Integer representation. Try this and see what happens, even though the target type is an int: func main() { This works fine: func main() { As does this: func main() { I guess the question is what should happen to this? func main() {
But it already allows for that as described above. Type conversion already occurs when you describe a struct field as: A int The only question we are left with is should "" be parsed as "0"? I would say no, as "" is not a valid integer representation. |
@fcntl I think I"m of the opinion that |
I think an argument could be made for parsing
as the "zero" value of a string is |
I think there's value in retaining consistency when encoding/decoding between different types in the stdlib, otherwise the behavior of parsing integers becomes unintuitive. The Example: _, err := strconv.Atoi("")
fmt.Println(err) Outputs:
|
Since the empty string is not the string form of any integer and the code has worked this way since 2012, we're not going to change this. |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go1.8
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?amd64
What did you do?
https://play.golang.org/p/IRHV8RRSoy
What did you expect to see?
json.Unmarshal should return nil
What did you see instead?
If try to unmarshal an empty string, it should return 0, instead of an error
The text was updated successfully, but these errors were encountered: