From 8e854178d0c1db079b80b34788c7b91dd7c003bc Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sat, 8 Aug 2020 13:43:24 +0900 Subject: [PATCH] Fix nested map --- decode_map.go | 2 ++ decode_test.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/decode_map.go b/decode_map.go index cd36587..6dd2695 100644 --- a/decode_map.go +++ b/decode_map.go @@ -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] != ',' { diff --git a/decode_test.go b/decode_test.go index 61d004a..28683b8 100644 --- a/decode_test.go +++ b/decode_test.go @@ -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 {