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: missing error unmarshalling null value into field with string tag #7046

Closed
gopherbot opened this issue Jan 1, 2014 · 7 comments
Milestone

Comments

@gopherbot
Copy link

by accipiter:

What steps will reproduce the problem?
Run: http://play.golang.org/p/o8o3NuIiu4

What is the expected output?
json: invalid use of ,string struct tag, trying to unmarshal "" into int
{A:4 B:2 C:0}

What do you see instead?
{A:4 B:2 C:4}

Which version are you using?  (run 'go version')
go version go1.2 windows/amd64

Please provide any additional information below.
The problem occurs when json.Unmarshal unmarshals into multiple struct fields (not
necessarily within the same struct) where the string field tag option is used. If a json
null value is encountered instead of a string, the package uses the value from previous
field with string option instead of returning an error.
If no previous value has been unmarshalled into such a field when null is encountered,
the expected error is returned.
@davecheney
Copy link
Contributor

Comment 1:

https://golang.org/cl/47260043/

Labels changed: added release-go1.3.

Status changed to Started.

@bradfitz
Copy link
Contributor

bradfitz commented Jan 3, 2014

Comment 2:

This issue was closed by revision 880442f.

Status changed to Fixed.

@gopherbot
Copy link
Author

Comment 3 by nicolashillegeer:

I can confirm that this problem also appears for float64 types. Perhaps also add a test
for that? (Don't know if this fix also fixes float64).

@gopherbot
Copy link
Author

Comment 4 by nicolashillegeer:

And as an example test case: http://play.golang.org/p/mT8l5jeN4c

@bradfitz
Copy link
Contributor

Comment 5:

This issue is closed.
Please open a new issue if you find a new bug with the latest (tip) version of Go.

@gopherbot
Copy link
Author

Comment 6 by nicolashillegeer:

Ok, I hear from someone that has go tip installed that the expected error is reported
now. Which is a shame in my opinion (while being unable to specify to just leave the
default value of the field when a null value is encountered).
Thanks!

@rsc
Copy link
Contributor

rsc commented Oct 7, 2014

Comment 7:

It was certainly a bug that unmarshaling {"a": "4", "b": 2, "c": null} produced {A:4,
B:2, C:4}. However, I don't believe the fix here was correct. It should have ignored the
null, not made it an error. This is consistent with the non-,string unmarshaling, as
amended due to issue #2540. I am going to change this behavior as part of fixing issue
8587. That should make nicolashillegeer happy at least.

@rsc rsc added this to the Go1.3 milestone Apr 14, 2015
@rsc rsc removed the release-go1.3 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
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