encoding/json: clarify Marshal behavior for string keys that implement encoding.TextMarshaler #28827
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: