-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/json: clarify Marshal behavior for string keys that implement encoding.TextMarshaler #28827
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
Comments
If it's a valid bug, I can fix it. I think about or checking for interface implementation beforehand, or mv Kind() to Type(). But the second variant seems quite dangerous. |
cc @rsc |
See the discussion when TextMarshaler support was added for map keys: #12146. There was concerns about backwards compatibility for existing kind strings that implement TextMarshaler. This may have to be a Go2 request. |
Oh, I have tried to find discussion by "TextMarshaler" keyword, but failed. Thanks! So what shoud I do now? Add a go2 request explicitly? |
This is mentioned in the documentation (https://tip.golang.org/pkg/encoding/json/#Marshal):
We could certainly be more explicit about the fact that “string keys” means “keys of any string type” rather than “keys of type |
I think it would help. Because basically you can not put your string based type instead a string without an explicit conversion. |
Change https://golang.org/cl/188417 mentions this issue: |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What did you do?
https://play.golang.org/p/uBmsW13USTc
tldr:
Before checking for encoding.TextMarshaler implementation the resolve method makes this check
so if out type of key is something like:
resolve ignores the encoding.TextMarshaler implementation.
go/src/encoding/json/encode.go
Line 864 in 969b9d8
What did you expect to see?
encoding.TextMarshaler is called for string types.
What did you see instead?
Just base string value.
/I have made small proof for myself:
https://play.golang.org/p/CFRVA4yDUlA
The text was updated successfully, but these errors were encountered: