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 to nil pointer documentation discrepancy #18730

Closed
JovanCe opened this issue Jan 20, 2017 · 2 comments

Comments

Projects
None yet
5 participants
@JovanCe
Copy link

commented Jan 20, 2017

When trying to unmarshal a json string into a nil pointer, the operation fails as can be seen here:
https://play.golang.org/p/wL6UDpaJ-h

The error returned is InvalidUnmarshalError whose docs clearly state:
"The argument to Unmarshal must be a non-nil pointer." (https://golang.org/pkg/encoding/json/#InvalidUnmarshalError)

But the docs for the Unmarshal function state:
"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."
(https://golang.org/pkg/encoding/json/#Unmarshal)

Obviously, the behaviour implemented is the one from the error docs.

Curiously, this works:
https://play.golang.org/p/w-Sg24qcCo
So, a reference to a nil pointer is accepted but the nil pointer itself is not. Anyone care to clarify?

go1.7.4 darwin/amd64

@josharian josharian changed the title Json Unmarshal to nil pointer documentation discrepancy encoding/json: Unmarshal to nil pointer documentation discrepancy Jan 20, 2017

@rakyll rakyll added this to the Go1.9 milestone Jan 20, 2017

@shantuo

This comment has been minimized.

Copy link
Contributor

commented Feb 7, 2017

My understanding is that this is a documentation bug. The behavior of returning an InvalidUnmarshalError looks work-as-intended. I guess the docs for the Unmarshal function means "If the pointer points to nil"? Because Unmarshal cannot modify the passed nil pointer.

@shantuo shantuo self-assigned this Feb 7, 2017

@gopherbot

This comment has been minimized.

Copy link

commented Feb 7, 2017

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

@gopherbot gopherbot closed this in 4fafc84 Feb 10, 2017

@golang golang locked and limited conversation to collaborators Feb 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.