Skip to content

Commit a6c3a47

Browse files
authored
Merge pull request #10 from goccy/feature/fix-nested-map
Fix handling of end character in map
2 parents 1aad6b6 + 8e85417 commit a6c3a47

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

decode_map.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func (d *mapDecoder) decodeStream(s *stream, p uintptr) error {
8686
}
8787
if s.char() == '}' {
8888
*(*unsafe.Pointer)(unsafe.Pointer(p)) = mapValue
89+
s.cursor++
8990
return nil
9091
}
9192
if s.char() != ',' {
@@ -148,6 +149,7 @@ func (d *mapDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error)
148149
cursor = skipWhiteSpace(buf, valueCursor)
149150
if buf[cursor] == '}' {
150151
*(*unsafe.Pointer)(unsafe.Pointer(p)) = mapValue
152+
cursor++
151153
return cursor, nil
152154
}
153155
if buf[cursor] != ',' {

decode_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ func Test_Decoder(t *testing.T) {
104104
assertEq(t, "map.b", v["b"], 2)
105105
assertEq(t, "map.c", v["c"], 3)
106106
assertEq(t, "map.d", v["d"], 4)
107+
t.Run("nested map", func(t *testing.T) {
108+
// https://github.com/goccy/go-json/issues/8
109+
content := `
110+
{
111+
"a": {
112+
"nestedA": "value of nested a"
113+
},
114+
"b": {
115+
"nestedB": "value of nested b"
116+
},
117+
"c": {
118+
"nestedC": "value of nested c"
119+
}
120+
}`
121+
var v map[string]interface{}
122+
assertErr(t, json.Unmarshal([]byte(content), &v))
123+
assertEq(t, "length", 3, len(v))
124+
})
107125
})
108126
t.Run("struct", func(t *testing.T) {
109127
type T struct {

0 commit comments

Comments
 (0)