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: decoder does not prefer exact match #66589

Closed
andrealancichinetti opened this issue Mar 29, 2024 · 2 comments
Closed

encoding/json: decoder does not prefer exact match #66589

andrealancichinetti opened this issue Mar 29, 2024 · 2 comments

Comments

@andrealancichinetti
Copy link

andrealancichinetti commented Mar 29, 2024

Go version

go1.21.4 darwin/arm64 (but also go.1.22)

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/andrea/Library/Caches/go-build'
GOENV='/Users/andrea/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/andrea/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/andrea/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.4'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/andrea/Desktop/work/cedar/tms/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/l1/f52v6_6x7r3898mqkgxf_pbm0000gn/T/go-build4023306156=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

https://go.dev/play/p/vOphzSORsXL

What did you see happen?

error: {not an exact match with tag}

What did you expect to see?

I have a struct with a field whose tag is an exact match of the JSON key. However, the decoder picks the JSON key with a case-insensitive match.

@seankhliao
Copy link
Member

the matching is a many to one mapping from json data to struct fields, in the order that the data is presented, so length matches and its value is assigned to the field, then it sees Length, which also matches the field so the data is overwritten (it doesn't record whether a field was seen before). You can observe the effect if you switch the order of the fields in json.

unfortunate, but I believe this is working as intended.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 29, 2024
@andrealancichinetti
Copy link
Author

Thanks Sean, I have now found out that there is a whole discussion about this here: #63397, which is pretty interesting

@golang golang locked and limited conversation to collaborators Mar 29, 2025
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

3 participants