From 1926476780685fefbf60893c5bf0b527a9e22d5e Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Tue, 9 Feb 2021 17:56:27 +0100 Subject: [PATCH] Wait for io.EOF when decoding a stream, fixes #123 --- decode_stream.go | 2 +- stream_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/decode_stream.go b/decode_stream.go index 561de91..f54400d 100644 --- a/decode_stream.go +++ b/decode_stream.go @@ -75,7 +75,7 @@ func (s *stream) read() bool { buf[last] = nul n, err := s.r.Read(buf[:last]) s.length = s.cursor + int64(n) - if n < last || err == io.EOF { + if err == io.EOF { s.allRead = true } else if err != nil { return false diff --git a/stream_test.go b/stream_test.go index 8fee6cd..43588e4 100644 --- a/stream_test.go +++ b/stream_test.go @@ -6,6 +6,7 @@ package json_test import ( "bytes" + "compress/gzip" "io" "io/ioutil" "log" @@ -463,3 +464,41 @@ func TestHTTPDecoding(t *testing.T) { t.Errorf("err = %v; want io.EOF", err) } } + +func TestGzipStreaming(t *testing.T) { + type someStruct struct { + ID int `json:"id"` + Text []string `json:"text"` + } + + manyItems := strings.Repeat(`"Quis autem vel eum iure reprehenderit, qui in ea voluptate velit esse, quam`+ + ` nihil molestiae consequatur, vel illum, qui dolorem eum fugiat, quo voluptas nulla pariatur?",`, 400) + + longJSON := `{"id":123,"text":[` + manyItems[0:len(manyItems)-1] + `]}` + + compressed := bytes.NewBuffer(nil) + gw := gzip.NewWriter(compressed) + + _, err := io.Copy(gw, bytes.NewReader([]byte(longJSON))) + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + err = gw.Close() + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + gr, err := gzip.NewReader(bytes.NewReader(compressed.Bytes())) + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + var v someStruct + dec := json.NewDecoder(gr) + + err = dec.Decode(&v) + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } +}