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: TextMarshaler type map key is not supported #38940

Closed
AllenX2018 opened this issue May 8, 2020 · 2 comments
Closed

encoding/json: TextMarshaler type map key is not supported #38940

AllenX2018 opened this issue May 8, 2020 · 2 comments

Comments

@AllenX2018
Copy link

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

$ go version
go version go1.14.1 windows/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows

What did you do?

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

What did you expect to see?

Output:{"MANUAL__aaa":"bbb"}

What did you see instead?

Output:{"aaa":"bbb"}

@mvdan
Copy link
Member

mvdan commented May 8, 2020

This is on purpose and documented; see #28827.

@mvdan mvdan closed this as completed May 8, 2020
@mvdan
Copy link
Member

mvdan commented May 8, 2020

Here's the order, as per the docs:

- keys of any string type are used directly
- encoding.TextMarshalers are marshaled
- integer keys are converted to strings

To force json to use your marshaler, don't let your type be a string type: https://play.golang.org/p/xMmVspIdMMS

You could argue the first two options should be reversed in the order, but I don't think we can change the documented behavior at this point without breaking a good portion of users.

@golang golang locked and limited conversation to collaborators May 8, 2021
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