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: MarshalJSON not called in value contexts for integer types #23263

Closed
schani opened this issue Dec 27, 2017 · 2 comments
Closed

encoding/json: MarshalJSON not called in value contexts for integer types #23263

schani opened this issue Dec 27, 2017 · 2 comments

Comments

@schani
Copy link

@schani schani commented Dec 27, 2017

This behavior is counterintuitive and requires bookkeeping to figure out which types are in a value context under which circumstances.

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

go version go1.9.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/schani/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/n9/hkp85g8d1_ddvqjfd9b_2b1h0000gn/T/go-build054789224=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

https://play.golang.org/p/HuQg9qdj4HE

What did you expect to see?

{"map":{"a":"string"},"direct":"string"}
{"map":{"a":"string"},"direct":"string"}

What did you see instead?

{"map":{"a":0},"direct":0}
{"map":{"a":0},"direct":"string"}
schani added a commit to quicktype/quicktype that referenced this issue Dec 27, 2017
It's a Go bug:

golang/go#23263
@dsnet

This comment has been minimized.

Copy link
Member

@dsnet dsnet commented Dec 27, 2017

The issue is that MarshalJSON is defined as a method on the pointer version of the integer. It should probably be the value receiver. Closing as duplicate of #22967

@dsnet dsnet closed this Dec 27, 2017
@bradfitz

This comment has been minimized.

Copy link
Contributor

@bradfitz bradfitz commented Dec 28, 2017

Also note that elements in maps aren't addressable so any typical automatic T to *T receiver switch sugar doesn't happen since the map element can't have its pointer addr taken.

schani added a commit to quicktype/quicktype that referenced this issue Feb 27, 2018
In the case where you had a class type with a union property, and that
class type was the value type of a map, the union wouldn't use the
custom unmarshaller, because of complicated Go stuff:
golang/go#23263 (comment)
@golang golang locked and limited conversation to collaborators Dec 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.