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: key type implementing json.Unmarshaler cannot be unmarshaled (documentation bug) #67495

Closed
bserdar opened this issue May 18, 2024 · 2 comments
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@bserdar
Copy link

bserdar commented May 18, 2024

Go version

go version go1.22.1 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/bserdar/.cache/go-build'
GOENV='/home/bserdar/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/bserdar/go/pkg/mod'
GONOPROXY='gitlab.com'
GONOSUMDB='gitlab.com'
GOOS='linux'
GOPATH='/home/bserdar/go'
GOPRIVATE='gitlab.com'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.1'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
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 -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3704543572=/tmp/go-build -gno-record-gcc-switches'

What did you do?

The json unmarshal documentation claims that:

The map's key type must either be any string type, an integer, implement json.Unmarshaler, or implement encoding.TextUnmarshaler.

So I did this:

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

What did you see happen?

A key type implementing json.Unmarshaler cannot be unmarshaled. If key type implements TextUnmarshaler, JSON unmarshaling works as expected.

What did you expect to see?

This is probably a documentation issue. The documentation for json.Marshal does not require the key type to implement json.Marshaler, but the documentation for json.Unmarshal claims it should work. So I believe the documentation for json.Unmarshal should be corrected to not say a key type implementation should implement json.Unmarshaler.

@seankhliao seankhliao added the NeedsFix The path to resolution is known, but the work has not been done. label May 18, 2024
@seankhliao
Copy link
Member

Looks like the docs for Marshal are correct, but Unmarshal is wrong.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/593335 mentions this issue: encoding/json: clarify the map's key type for Unmarshal

@dmitshur dmitshur added this to the Go1.23 milestone Jun 19, 2024
Mchnan pushed a commit to Mchnan/go-sylixos that referenced this issue Jul 9, 2024
While here, also fix doc link for encoding.TextMarshaler.

Fixes golang#67495

Change-Id: Ia2a674c5c35b5a849ce8f5eef3d34d165b3195b6
Reviewed-on: https://go-review.googlesource.com/c/go/+/593335
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Joseph Tsai <[email protected]>
Reviewed-by: David Chase <[email protected]>
Auto-Submit: Ian Lance Taylor <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants