aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/json/decode.go
diff options
context:
space:
mode:
authorCuong Manh Le <cuong.manhle.vn@gmail.com>2019-10-10 10:19:12 +0700
committerDaniel Martí <mvdan@mvdan.cc>2019-10-10 12:59:11 +0000
commitfb9af8411eaa3e38d2f72e28a305772f50042657 (patch)
treef09dadcefa10828ec8c8e7364c4d1383c67748a9 /src/encoding/json/decode.go
parent900ebcfe4d592486dd5bc50f5e8101ba65e32948 (diff)
downloadgo-fb9af8411eaa3e38d2f72e28a305772f50042657.tar.xz
encoding/json: support TextUnmarshaler for map keys with string underlying types
When unmarshaling to a map, the map's key type must either be a string, an integer, or implement encoding.TextUnmarshaler. But for a user defined type, reflect.Kind will not distinguish between the static type and the underlying type. In: var x MyString = "x" t := reflect.TypeOf(x) println(t.Kind() == reflect.String) the Kind of x is still reflect.String, even though the static type of x is MyString. Moreover, checking for the map's key type is a string occurs first, so even if the map key type MyString implements encoding.TextUnmarshaler, it will be ignored. To fix the bug, check for encoding.TextUnmarshaler first. Fixes #34437 Change-Id: I780e0b084575e1dddfbb433fe03857adf71d05fb Reviewed-on: https://go-review.googlesource.com/c/go/+/200237 Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Diffstat (limited to 'src/encoding/json/decode.go')
-rw-r--r--src/encoding/json/decode.go4
1 files changed, 2 insertions, 2 deletions
diff --git a/src/encoding/json/decode.go b/src/encoding/json/decode.go
index 407fbcedbe..86d8a69db7 100644
--- a/src/encoding/json/decode.go
+++ b/src/encoding/json/decode.go
@@ -773,14 +773,14 @@ func (d *decodeState) object(v reflect.Value) error {
kt := t.Key()
var kv reflect.Value
switch {
- case kt.Kind() == reflect.String:
- kv = reflect.ValueOf(key).Convert(kt)
case reflect.PtrTo(kt).Implements(textUnmarshalerType):
kv = reflect.New(kt)
if err := d.literalStore(item, kv, true); err != nil {
return err
}
kv = kv.Elem()
+ case kt.Kind() == reflect.String:
+ kv = reflect.ValueOf(key).Convert(kt)
default:
switch kt.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: