go icon indicating copy to clipboard operation
go copied to clipboard

encoding/json: wrong type name used in UnmarshalTypeError

Open j2gg0s opened this issue 6 months ago • 4 comments

Go version

go version go1.22.4 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/j2gg0s/Library/Caches/go-build'
GOENV='/Users/j2gg0s/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/j2gg0s/go/pkg/mod'
GONOPROXY='dev.msh.team,ohai.bot,github.com/j2gg0s'
GONOSUMDB='dev.msh.team,ohai.bot,github.com/j2gg0s'
GOOS='darwin'
GOPATH='/Users/j2gg0s/go'
GOPRIVATE='dev.msh.team,ohai.bot,github.com/j2gg0s'
GOPROXY='https://goproxy.cn,direct'
GOROOT='/Users/j2gg0s/sdk/go1.22.4'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='go1.22.4'
GOTOOLDIR='/Users/j2gg0s/sdk/go1.22.4/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.4'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/j2gg0s/gosrc/src/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/3g/291pfdd54f11rlb15_hbq1dr0000gn/T/go-build1558397162=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

package main

import (
	"encoding/json"
	"fmt"
)

type Y struct {
	ZField int
}

type X struct {
	YField Y
}

func ExampleUnmarshalTypeError() {
	x := X{}
	if err := json.Unmarshal([]byte(`{"YField": {"ZField": "hello"}}`), &x); err != nil {
		fmt.Println(err)
	}
	// Output:
	// json: cannot unmarshal string into Go struct field Y.YField.ZField of type int
}

What did you see happen?

Error's message is json: cannot unmarshal string into Go struct field Y.YField.ZField of type int

What did you expect to see?

Error's message should bejson: cannot unmarshal string into Go struct field X.YField.ZField of type int

If my understanding is correct, can I submit a PR to fix this issue?

j2gg0s avatar Aug 19 '24 09:08 j2gg0s