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

fix: migrate code to support map int #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,14 +622,20 @@ func (d *decodeState) object(v reflect.Value) {
// map[string]T or map[encoding.TextUnmarshaler]T
switch v.Kind() {
case reflect.Map:
// Map key must either have string kind or be an encoding.TextUnmarshaler.
// Map key must either have string kind, have an integer kind,
// or be an encoding.TextUnmarshaler.
t := v.Type()
if t.Key().Kind() != reflect.String &&
!reflect.PtrTo(t.Key()).Implements(textUnmarshalerType) {
d.saveError(&UnmarshalTypeError{"object", v.Type(), int64(d.off)})
d.off--
d.next() // skip over { } in input
return
switch t.Key().Kind() {
case reflect.String,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
default:
if !reflect.PtrTo(t.Key()).Implements(textUnmarshalerType) {
d.saveError(&UnmarshalTypeError{Value: "object", Type: t, Offset: int64(d.off)})
d.off--
d.next() // skip over { } in input
return
}
}
if v.IsNil() {
v.Set(reflect.MakeMap(t))
Expand Down Expand Up @@ -745,9 +751,30 @@ func (d *decodeState) object(v reflect.Value) {
d.literalStore(key, kv, true, true)
kv = kv.Elem()
default:
panic("json: Unexpected key type") // should never occur
switch kt.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
s := string(key)
n, err := strconv.ParseInt(s, 10, 64)
if err != nil || reflect.Zero(kt).OverflowInt(n) {
d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: kt, Offset: int64(start + 1)})
break
}
kv = reflect.ValueOf(n).Convert(kt)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
s := string(key)
n, err := strconv.ParseUint(s, 10, 64)
if err != nil || reflect.Zero(kt).OverflowUint(n) {
d.saveError(&UnmarshalTypeError{Value: "number " + s, Type: kt, Offset: int64(start + 1)})
break
}
kv = reflect.ValueOf(n).Convert(kt)
default:
panic("json: Unexpected key type") // should never occur
}
}
if kv.IsValid() {
v.SetMapIndex(kv, subv)
}
v.SetMapIndex(kv, subv)
}

// Next token must be , or }.
Expand Down
10 changes: 10 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1359,3 +1359,13 @@ func TestDecodeSingleQuoteStringInterface(t *testing.T) {
t.Errorf("Unmarshal = %q; want %q", got, want)
}
}

func TestUnmarshalMapIntKey(t *testing.T) {
m := make(map[int]int)
if err := Unmarshal([]byte(`{"1":1}`), &m); err != nil {
t.Fatalf("Unmarshal: %v", err)
}
if m[1] != 1 {
t.Fatalf("Did not write value")
}
}