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: unexpected behavior when unmarshalling to "boxed" struct #33993

Closed
brenol opened this issue Aug 31, 2019 · 5 comments
Closed

encoding/json: unexpected behavior when unmarshalling to "boxed" struct #33993

brenol opened this issue Aug 31, 2019 · 5 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@brenol
Copy link

brenol commented Aug 31, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12.9 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/goworkspace"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build909978713=/tmp/go-build -gno-record-gcc-switches"

What did you do?

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

What did you expect to see?

I did not expect v to be of type map[string]interface{}, as its underlying type is clearly Y.

What did you see instead?

vis of type map[string]interface{}, failing the type assertion.

Note that if it's a pointer to Y, it works correctly.
Also, this came from a bigger project where Sprintf was reporting both vand Y{} with the same underlying type, however, I was unable to reproduce that here.

Also, removing json.Unmarshal fixes the issue, as the boxed Y is cast correctly to Y when there is no unmarshal.

@OneOfOne
Copy link
Contributor

You need to use a pointer to Y:

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

@katiehockman katiehockman added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Sep 3, 2019
@katiehockman
Copy link
Contributor

@brenol can you confirm that this suggestion resolves your issue? If so, this issue can be closed.

You can also check out https://github.com/golang/go/wiki/Questions for more resources.

@brenol
Copy link
Author

brenol commented Sep 3, 2019

Yes, the suggestion fixes the issue.

However, is this the expected behavior? I mean, as I mentioned, removing json.Unmarshal altogether does work and it does not panic.

My main issue here is: should json.Unmarshal change the underlying type of a by value struct to map[string]interface{}? I tried looking up for some reason this happens but it didn't make sense to me, as the docs don't make it clear why it works with pointers and not with values.

@katiehockman katiehockman added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Sep 25, 2019
@katiehockman
Copy link
Contributor

/cc @rsc

@seankhliao
Copy link
Member

Plain structs in interfaces aren't addressable, so they can't be set.
encoding/json makes no provision for setting the value of non pointer structs (since it can't), so it follows the other documented rules, allocating a map.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Aug 27, 2022
@golang golang locked and limited conversation to collaborators Aug 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants