Skip to content

affected/package: encoding/json #54609

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

Closed
madkins23 opened this issue Aug 22, 2022 · 1 comment
Closed

affected/package: encoding/json #54609

madkins23 opened this issue Aug 22, 2022 · 1 comment

Comments

@madkins23
Copy link

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

$ go version
go version go1.18.5 linux/amd64

Does this issue reproduce with the latest release?

AFAIK I'm using the latest release (or at least the latest snap release).

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN="/home/marc/go/bin"
GOCACHE="/home/marc/.cache/go-build"
GOENV="/home/marc/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/marc/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/marc/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/snap/go/9952"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/snap/go/9952/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3232480261=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Given a container struct with a map and an entity struct with custom Marshaler and Unmarshaler methods. The custom methods are defined using pointer references to the entity struct.

If the map values in the container struct are pointers to the entity struct everything works fine. If the map values are the actual entity structs then the MarshalJSON method isn't called.

Array values seem to work just fine with both pointers and objects.

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

What did you expect to see?

The map value cases for pointer and object should be the same.

What did you see instead?

The Object case doesn't seem to call the custom MarshalJSON for map values that are objects, but it works for array values that are objects.

Ptr container:
  MarshalJSON()  'listed'->1
  MarshalJSON()  'mapped'->2
  UnmarshalJSON()  {"Text":"listed|1"}
  UnmarshalJSON()  {"Text":"mapped|2"}
! ptr == ptr2
  ptr1: {['listed'->1] map[ptrKey:'mapped'->2]}
  ptr2: {['listed'->1] map[ptrKey:'mapped'->2]}
Obj container:
  MarshalJSON()  'listed'->1
  UnmarshalJSON()  {"Text":"listed|1"}
  UnmarshalJSON()  {}
Error unmarshaling objContainer: wrong number of pieces: 1
* obj1 != obj2
  obj1: {[{listed 1}] map[objKey:{mapped 2}]}
  obj2: {[{listed 1}] map[]}
@seankhliao
Copy link
Member

UnmarshalJSON is defined on *Example, not Example, and map values are unaddressable.

Closing as working as intended.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Aug 22, 2022
@golang golang locked and limited conversation to collaborators Aug 22, 2023
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