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: encoding duplicate struct keys fail silently (and does not report error) #17913

Closed
mlitvin opened this issue Nov 14, 2016 · 2 comments

Comments

Projects
None yet
4 participants
@mlitvin
Copy link

commented Nov 14, 2016

When due to a bug a structure has two fields with the same key like:

type bug struct {
	Key   string `json:"bug"`
	Value string `json:"bug"`
}

The JSON marshaller will silently ignore the fields. I would expect it to return an error to the caller.

Example program: https://play.golang.org/p/DOUbRT9k_U

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

go version go1.7 darwin/amd64

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/moshe/Documents/code/gopath"
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/y6/cd3mw6tx1qn89g0vn2pdz4gc0000gn/T/go-build725917179=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"

What did you do?

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

What did you expect to see?

I expected to get an error from json.Marshal

What did you see instead?

No error and no encoding

@0xmohit

This comment has been minimized.

Copy link
Contributor

commented Nov 14, 2016

This appears to be by design; see https://golang.org/cl/8617044.

Also from the documentation:

Otherwise there are multiple fields, and all are ignored; no error occurs.

@0xmohit

This comment has been minimized.

Copy link
Contributor

commented Nov 14, 2016

vet at tip detects duplicate struct tags (#17599).

@quentinmit quentinmit changed the title encoding/json encoding duplicate struct keys fail silently (and does not report error) encoding/json: encoding duplicate struct keys fail silently (and does not report error) Nov 15, 2016

@quentinmit quentinmit closed this Nov 15, 2016

@golang golang locked and limited conversation to collaborators Nov 16, 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.