Merge pull request #10 from goccy/feature/fix-nested-map

Fix handling of end character in map
This commit is contained in:
Masaaki Goshima 2020-08-08 13:45:21 +09:00 committed by GitHub
commit a6c3a47779
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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() == '}' { 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] != ',' {

View File

@ -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 {