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

json.Marshal ignores all fields exept embedded struct of type json.RawMessage #31167

Closed
sirbralex opened this issue Mar 30, 2019 · 2 comments
Closed

Comments

@sirbralex
Copy link

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

$ go version
go version go1.12.1 windows/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
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Aleksandr\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\GoProjects
set GOPROXY=
set GORACE=
set GOROOT=D:\Program Files\Go
set GOTMPDIR=
set GOTOOLDIR=D:\Program Files\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\ALEKSA~1\AppData\Local\Temp\go-build286887446=/tmp/go-build -gno-record-gcc-switches

What did you do?

https://play.golang.org/p/5q8nGlLBDVy

What did you expect to see?

{"Title":"Programming in Go","Name":"Caleb Doxsey","Price":"20$"}

What did you see instead?

{"Price":"20$"}

@fraenkel
Copy link
Contributor

@sirbralex json.Marshal is invoking the MarshalJson promoted via the embedded json.RawMessage.

You can read more here, https://golang.org/ref/spec#Struct_types

@dsnet
Copy link
Member

dsnet commented Mar 31, 2019

This is an unfortunate gotcha of embedding in Go and there's not much that can be done to encoding/json. Addressing this requires some level of language change.

See #30148 (for related issue for json) and #22013 (for related issue about embedding in general).

@dsnet dsnet closed this as completed Mar 31, 2019
@golang golang locked and limited conversation to collaborators Mar 30, 2020
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

4 participants