mirror of https://github.com/goccy/go-json.git
Fix map key type
This commit is contained in:
parent
c9f1d0043e
commit
a9b9597c2e
|
@ -29,7 +29,7 @@ func (d *interfaceDecoder) numDecoder(s *stream) decoder {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
interfaceMapType = type2rtype(
|
interfaceMapType = type2rtype(
|
||||||
reflect.TypeOf((*map[interface{}]interface{})(nil)).Elem(),
|
reflect.TypeOf((*map[string]interface{})(nil)).Elem(),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,12 +38,12 @@ func (d *interfaceDecoder) decodeStream(s *stream, p uintptr) error {
|
||||||
for {
|
for {
|
||||||
switch s.char() {
|
switch s.char() {
|
||||||
case '{':
|
case '{':
|
||||||
var v map[interface{}]interface{}
|
var v map[string]interface{}
|
||||||
ptr := unsafe.Pointer(&v)
|
ptr := unsafe.Pointer(&v)
|
||||||
d.dummy = ptr
|
d.dummy = ptr
|
||||||
if err := newMapDecoder(
|
if err := newMapDecoder(
|
||||||
interfaceMapType,
|
interfaceMapType,
|
||||||
newInterfaceDecoder(d.typ),
|
newStringDecoder(),
|
||||||
newInterfaceDecoder(d.typ),
|
newInterfaceDecoder(d.typ),
|
||||||
).decodeStream(s, uintptr(ptr)); err != nil {
|
).decodeStream(s, uintptr(ptr)); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -120,12 +120,12 @@ func (d *interfaceDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, e
|
||||||
cursor = skipWhiteSpace(buf, cursor)
|
cursor = skipWhiteSpace(buf, cursor)
|
||||||
switch buf[cursor] {
|
switch buf[cursor] {
|
||||||
case '{':
|
case '{':
|
||||||
var v map[interface{}]interface{}
|
var v map[string]interface{}
|
||||||
ptr := unsafe.Pointer(&v)
|
ptr := unsafe.Pointer(&v)
|
||||||
d.dummy = ptr
|
d.dummy = ptr
|
||||||
dec := newMapDecoder(
|
dec := newMapDecoder(
|
||||||
interfaceMapType,
|
interfaceMapType,
|
||||||
newInterfaceDecoder(d.typ),
|
newStringDecoder(),
|
||||||
newInterfaceDecoder(d.typ),
|
newInterfaceDecoder(d.typ),
|
||||||
)
|
)
|
||||||
cursor, err := dec.decode(buf, cursor, uintptr(ptr))
|
cursor, err := dec.decode(buf, cursor, uintptr(ptr))
|
||||||
|
|
|
@ -204,7 +204,7 @@ func Test_Decoder(t *testing.T) {
|
||||||
var v interface{}
|
var v interface{}
|
||||||
assertErr(t, json.Unmarshal([]byte(`{"a": 1, "b": "c"}`), &v))
|
assertErr(t, json.Unmarshal([]byte(`{"a": 1, "b": "c"}`), &v))
|
||||||
assertEq(t, "interface.kind", "map", reflect.TypeOf(v).Kind().String())
|
assertEq(t, "interface.kind", "map", reflect.TypeOf(v).Kind().String())
|
||||||
m := v.(map[interface{}]interface{})
|
m := v.(map[string]interface{})
|
||||||
assertEq(t, "interface", `1`, fmt.Sprint(m["a"]))
|
assertEq(t, "interface", `1`, fmt.Sprint(m["a"]))
|
||||||
assertEq(t, "interface", `c`, fmt.Sprint(m["b"]))
|
assertEq(t, "interface", `c`, fmt.Sprint(m["b"]))
|
||||||
})
|
})
|
||||||
|
|
|
@ -108,8 +108,8 @@ func TestStructTagObjectKey(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unmarshal(%#q) failed: %v", b, err)
|
t.Fatalf("Unmarshal(%#q) failed: %v", b, err)
|
||||||
}
|
}
|
||||||
for i, v := range f.(map[interface{}]interface{}) {
|
for i, v := range f.(map[string]interface{}) {
|
||||||
switch i.(string) {
|
switch i {
|
||||||
case tt.key:
|
case tt.key:
|
||||||
if s, ok := v.(string); !ok || s != tt.value {
|
if s, ok := v.(string); !ok || s != tt.value {
|
||||||
t.Fatalf("Unexpected value: %#q, want %v", s, tt.value)
|
t.Fatalf("Unexpected value: %#q, want %v", s, tt.value)
|
||||||
|
|
Loading…
Reference in New Issue