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: Unmarshal([]byte(null), &value) does nothing #6724

Closed
gopherbot opened this issue Nov 6, 2013 · 7 comments
Closed

encoding/json: Unmarshal([]byte(null), &value) does nothing #6724

gopherbot opened this issue Nov 6, 2013 · 7 comments
Milestone

Comments

@gopherbot
Copy link

What steps will reproduce the problem?

http://play.golang.org/p/iVsT1IeNkU

This code:

    var s string = "foo"
    err := Unmarshal([]byte(`null`), &s)

does not change s, but also does not signal an error.

While this does make some sense, it surprised me; I expected either an error or zero
value. Error because `null` is not a string, zero value because that's what the logic is
elsewhere for null:

http://golang.org/pkg/encoding/json/#Unmarshal says

"To unmarshal JSON into a pointer, Unmarshal first handles the case of the JSON
being the JSON literal null. In that case, Unmarshal sets the pointer to nil. Otherwise,
Unmarshal unmarshals the JSON into the value pointed at by the pointer. If the pointer
is nil, Unmarshal allocates a new value for it to point to."

which means that unmarshaling `null` into a pointer actually changes it, and
unmarshaling `null` into a non-pointer doesn't.

I feel like the docs need an extra paragraph that talks about null.
@rsc
Copy link
Contributor

rsc commented Nov 7, 2013

Comment 1:

Or maybe the behavior is wrong. But probably just the docs need updating.

Labels changed: added priority-later, go1.3, removed priority-triage.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 2:

Labels changed: added release-go1.3.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 3:

Labels changed: removed go1.3.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 4:

Labels changed: added repo-main.

@gopherbot
Copy link
Author

Comment 5:

CL https://golang.org/cl/89600043 mentions this issue.

@gopherbot
Copy link
Author

Comment 6:

CL https://golang.org/cl/100430043 mentions this issue.

@rsc
Copy link
Contributor

rsc commented May 13, 2014

Comment 7:

This issue was closed by revision fc1e5a8.

Status changed to Fixed.

@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

2 participants