golang / go

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

encoding/json: include field name in error for decoding unquoted number into",string" tag #67774

Open kokizzu opened 1 month ago

kokizzu commented 1 month ago

Go version

all version affected

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/asd/.cache/go-build'
GOENV='/home/asd/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/asd/go/pkg/mod'
GOOS='linux'
GOPATH='/home/asd/go'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go-1.22'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go-1.22/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.3'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/tmp/a/go.mod'
GOWORK='off'
CGO_CFLAGS='-Wno-return-local-addr'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2911848713=/tmp/go-build -gno-record-gcc-switches'

What did you do?

try unmarshalling json quoted with int, with and without ",string", all combination only works

this is quite annoying if there's bunch of fields in the struct (eg. 24+) and the error super unclear json: invalid use of ,string struct tag, trying to unmarshal unquoted value into uint64, which field?

What did you see happen?

just json: invalid use of ,string struct tag, trying to unmarshal unquoted value into uint64

What did you expect to see?

either clearer error message showing which field like: json: invalid use of ,string struct tag, trying to unmarshal unquoted value into uint64 field MoneyIDR

or more permissive unmarshalling option that accept both quoted or unquoted number that targets to number field (int64/uint64) so dont have to use js.Number

seankhliao commented 1 month ago

similar to #22816