golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
121.18k stars 17.37k forks source link

encoding/json: no field information is shown when decoding a map fails #67210

Open zhiqiangxu opened 2 months ago

zhiqiangxu commented 2 months ago

Go version

go version go1.22.0 darwin/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/alanxu/Library/Caches/go-build'
GOENV='/Users/alanxu/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/alanxu/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/alanxu/go'
GOPRIVATE=''
GOPROXY='https://goproxy.cn'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/alanxu/Desktop/github/optimism/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 x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/bd/h02c_p7d6zq0t4tsxr3lkrt00000gn/T/go-build3885504692=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I'm decoding a map with many fields, but the error I got is:

json: cannot unmarshal hex string without 0x prefix into Go value of type common.Address

There's no information about which field is causing the error.

What did you see happen?

The error information when decoding map is vague, it doesn't indicate which field is causing the error.

What did you expect to see?

I expect to see which field(the specific key or value) is causing the error.

cherrymui commented 2 months ago

Could you share a specific example that reproduce the issue? Thanks.

zhiqiangxu commented 2 months ago

Could you share a specific example that reproduce the issue? Thanks.

Here is the minimal program to reproduce.

It reports: json: cannot unmarshal hex string without 0x prefix into Go value of type common.Hash

(In my actual case, the map has a lot of fields.)

zhiqiangxu commented 1 month ago

@cherrymui maybe the WaitingForInfo label can be removed?

cherrymui commented 1 month ago

Thanks.

cc @rsc @dsnet @bradfitz @mvdan

dsnet commented 1 month ago

The v2 JSON discussion (https://github.com/golang/go/discussions/63397) includes a JSONPointer field in jsontext.SyntacticError and json.SemanticError. Such a field would be used to store a JSON pointer (RFC 6901), that would describe exactly where an error occurred.

Support for populating this is planned, but not currently implemented in the prototype implementation at https://github.com/go-json-experiment/json.