diff --git a/encode_test.go b/encode_test.go index 41a14cc..c970188 100644 --- a/encode_test.go +++ b/encode_test.go @@ -1958,3 +1958,19 @@ func TestEncodeContextOption(t *testing.T) { } }) } + +func TestIssue251(t *testing.T) { + array := [3]int{1, 2, 3} + err := stdjson.Unmarshal([]byte("[ ]"), &array) + if err != nil { + t.Fatal(err) + } + t.Log(array) + + array = [3]int{1, 2, 3} + err = json.Unmarshal([]byte("[ ]"), &array) + if err != nil { + t.Fatal(err) + } + t.Log(array) +} diff --git a/internal/decoder/array.go b/internal/decoder/array.go index 7f9f1eb..7caaf75 100644 --- a/internal/decoder/array.go +++ b/internal/decoder/array.go @@ -111,8 +111,17 @@ func (d *arrayDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe return cursor, nil case '[': idx := 0 - for { + cursor++ + cursor = skipWhiteSpace(buf, cursor) + if buf[cursor] == ']' { + for idx < d.alen { + *(*unsafe.Pointer)(unsafe.Pointer(uintptr(p) + uintptr(idx)*d.size)) = d.zeroValue + idx++ + } cursor++ + return cursor, nil + } + for { if idx < d.alen { c, err := d.valueDecoder.Decode(ctx, cursor, depth, unsafe.Pointer(uintptr(p)+uintptr(idx)*d.size)) if err != nil { @@ -137,6 +146,7 @@ func (d *arrayDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe cursor++ return cursor, nil case ',': + cursor++ continue default: return 0, errors.ErrInvalidCharacter(buf[cursor], "array", cursor)