forked from mirror/go-json
Fix nested map
This commit is contained in:
parent
1aad6b6bfa
commit
8e854178d0
|
@ -86,6 +86,7 @@ func (d *mapDecoder) decodeStream(s *stream, p uintptr) error {
|
||||||
}
|
}
|
||||||
if s.char() == '}' {
|
if s.char() == '}' {
|
||||||
*(*unsafe.Pointer)(unsafe.Pointer(p)) = mapValue
|
*(*unsafe.Pointer)(unsafe.Pointer(p)) = mapValue
|
||||||
|
s.cursor++
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if s.char() != ',' {
|
if s.char() != ',' {
|
||||||
|
@ -148,6 +149,7 @@ func (d *mapDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error)
|
||||||
cursor = skipWhiteSpace(buf, valueCursor)
|
cursor = skipWhiteSpace(buf, valueCursor)
|
||||||
if buf[cursor] == '}' {
|
if buf[cursor] == '}' {
|
||||||
*(*unsafe.Pointer)(unsafe.Pointer(p)) = mapValue
|
*(*unsafe.Pointer)(unsafe.Pointer(p)) = mapValue
|
||||||
|
cursor++
|
||||||
return cursor, nil
|
return cursor, nil
|
||||||
}
|
}
|
||||||
if buf[cursor] != ',' {
|
if buf[cursor] != ',' {
|
||||||
|
|
|
@ -104,6 +104,24 @@ func Test_Decoder(t *testing.T) {
|
||||||
assertEq(t, "map.b", v["b"], 2)
|
assertEq(t, "map.b", v["b"], 2)
|
||||||
assertEq(t, "map.c", v["c"], 3)
|
assertEq(t, "map.c", v["c"], 3)
|
||||||
assertEq(t, "map.d", v["d"], 4)
|
assertEq(t, "map.d", v["d"], 4)
|
||||||
|
t.Run("nested map", func(t *testing.T) {
|
||||||
|
// https://github.com/goccy/go-json/issues/8
|
||||||
|
content := `
|
||||||
|
{
|
||||||
|
"a": {
|
||||||
|
"nestedA": "value of nested a"
|
||||||
|
},
|
||||||
|
"b": {
|
||||||
|
"nestedB": "value of nested b"
|
||||||
|
},
|
||||||
|
"c": {
|
||||||
|
"nestedC": "value of nested c"
|
||||||
|
}
|
||||||
|
}`
|
||||||
|
var v map[string]interface{}
|
||||||
|
assertErr(t, json.Unmarshal([]byte(content), &v))
|
||||||
|
assertEq(t, "length", 3, len(v))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
t.Run("struct", func(t *testing.T) {
|
t.Run("struct", func(t *testing.T) {
|
||||||
type T struct {
|
type T struct {
|
||||||
|
|
Loading…
Reference in New Issue