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: decoding a null value does not replace previous value #14640

Closed
AntiPaste opened this issue Mar 4, 2016 · 4 comments
Closed
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@AntiPaste
Copy link

Decoding a JSON value into an already populated struct replaces the previous value stored in the struct as long as the key is present.

Example of working behaviour:
http://play.golang.org/p/pGaGDSbVMy

However, if the decoded value is null the previous value is not replaced.

Example of incorrect behaviour:
http://play.golang.org/p/i25WkqFrW-

Is this intended behaviour for json.Unmarshal, json.Decoder.Decode & co.?

Additionally, if this is intended behaviour the json.Decoder.Decode example at golang.org might need fixing. It currently shows the following example which will exhibit incorrect results if either Name or Text is null: http://play.golang.org/p/wfoi8On0DX

A fixed version without possibility of incorrect results: http://play.golang.org/p/zVM9xzqIz4

Go version: go version go1.6 linux/amd64
Go environment:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="[redacted]"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"```
@ianlancetaylor ianlancetaylor changed the title encoding/json: Decoding a null value does not replace previous value encoding/json: decoding a null value does not replace previous value Mar 4, 2016
@ianlancetaylor ianlancetaylor added this to the Go1.7 milestone Mar 4, 2016
@cespare
Copy link
Contributor

cespare commented May 14, 2016

I believe this is working as intended and documented.

Unmarshaling null into a string intentionally does not change the string:

The JSON null value unmarshals into an interface, map, pointer, or slice by setting that Go value to nil. Because null is often used in JSON to mean “not present,” unmarshaling a JSON null into any other Go type has no effect on the value and produces no error.

Example without a struct: https://play.golang.org/p/MmRj0E1bvV

If your struct has one of the nullable types mentioned above, such as a pointer, then unmarshaling null will set your field to nil:

https://play.golang.org/p/vvXkL6gFiR

@rsc
Copy link
Contributor

rsc commented May 18, 2016

The docs @cespare wrote are very clear. I am not really a fan of this behavior but it's widely expected and too late to change.

@quentinmit quentinmit added Documentation NeedsFix The path to resolution is known, but the work has not been done. labels May 26, 2016
@quentinmit
Copy link
Contributor

It seems like this is just a matter of improving the example code, then. I sent CL 23483 for that.

@gopherbot
Copy link

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

@golang golang locked and limited conversation to collaborators May 26, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants