Fix nested map

This commit is contained in:
Masaaki Goshima 2020-08-08 13:43:24 +09:00
parent 1aad6b6bfa
commit 8e854178d0
2 changed files with 20 additions and 0 deletions

View File

@ -86,6 +86,7 @@ func (d *mapDecoder) decodeStream(s *stream, p uintptr) error {
}
if s.char() == '}' {
*(*unsafe.Pointer)(unsafe.Pointer(p)) = mapValue
s.cursor++
return nil
}
if s.char() != ',' {
@ -148,6 +149,7 @@ func (d *mapDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error)
cursor = skipWhiteSpace(buf, valueCursor)
if buf[cursor] == '}' {
*(*unsafe.Pointer)(unsafe.Pointer(p)) = mapValue
cursor++
return cursor, nil
}
if buf[cursor] != ',' {

View File

@ -104,6 +104,24 @@ func Test_Decoder(t *testing.T) {
assertEq(t, "map.b", v["b"], 2)
assertEq(t, "map.c", v["c"], 3)
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) {
type T struct {