From 7be58ac89df93e099b5a212ae588f47b653dbf07 Mon Sep 17 00:00:00 2001 From: Anders Brander Date: Mon, 13 Mar 2023 12:01:01 +0100 Subject: [PATCH] Fix mapDecoder.DecodeStream() to accept empty objects containing whitespace (#425) --- decode_test.go | 8 ++++++++ internal/decoder/map.go | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/decode_test.go b/decode_test.go index 9306b2e..df914ed 100644 --- a/decode_test.go +++ b/decode_test.go @@ -282,6 +282,14 @@ func Test_Decoder_DisallowUnknownFields(t *testing.T) { } } +func Test_Decoder_EmptyObjectWithSpace(t *testing.T) { + dec := json.NewDecoder(strings.NewReader(`{"obj":{ }}`)) + var v struct { + Obj map[string]int `json:"obj"` + } + assertErr(t, dec.Decode(&v)) +} + type unmarshalJSON struct { v int } diff --git a/internal/decoder/map.go b/internal/decoder/map.go index 7a6eea3..07a9cae 100644 --- a/internal/decoder/map.go +++ b/internal/decoder/map.go @@ -88,7 +88,7 @@ func (d *mapDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) erro mapValue = makemap(d.mapType, 0) } s.cursor++ - if s.equalChar('}') { + if s.skipWhiteSpace() == '}' { *(*unsafe.Pointer)(p) = mapValue s.cursor++ return nil