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: cannot use json:"-" to omit a shadowed field of an embedded type. #17016

Closed
bhcleek opened this issue Sep 7, 2016 · 3 comments

Comments

Projects
None yet
5 participants
@bhcleek
Copy link
Contributor

commented Sep 7, 2016

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

Go 1.7

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

Locally I use:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/bhcleek/develop/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/86/4lzvmk8s5mschgm8s7scwxhh0000gn/T/go-build783586116=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"

But the playground also exhibits this behavior.

What did you do?

Embed a struct, shadow one of its fields, and add the json:"-" tag to omit the shadowed field.

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

What did you expect to see?

I expected the json to not include a property for the shadowed field.

What did you see instead?

The shadowed field was included in the json.

After consideration, I believe the behavior is correct, because it allows the unmarshal case to work as one would expect, but the documentation doesn't lead one to expect the current behavior for marshalling.

@freeformz

This comment has been minimized.

Copy link
Contributor

commented Sep 7, 2016

@bhcleek The Value declared in WrapBar is not shadowing the Value declared in Bar.

Compare this to this to this

@bhcleek

This comment has been minimized.

Copy link
Contributor Author

commented Sep 7, 2016

@freeformz I just used the zero values in my example, but your second playground effectively demonstrates the same thing. Notice the properties present in the JSON strings, focusing less on which values are in them. In both my example and your second example, I originally expected the WrapBar json to not include the value property at all. This seems more like a documentation concern than anything else at this point.

@quentinmit quentinmit added this to the Go1.8Maybe milestone Sep 7, 2016

@odeke-em odeke-em changed the title Cannot use json:"-" to omit a shadowed field of an embedded type. encoding/json: cannot use json:"-" to omit a shadowed field of an embedded type. Sep 8, 2016

@quentinmit quentinmit added the NeedsFix label Oct 10, 2016

@rsc

This comment has been minimized.

Copy link
Contributor

commented Oct 20, 2016

The docs seem clear that - kills the current field, not that it kills any other fields.
If you want to kill the embedded value you can do so by making it unavailable by embedding another value at the same depth.

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

@rsc rsc closed this Oct 20, 2016

@golang golang locked and limited conversation to collaborators Oct 20, 2017

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.