Merge pull request #124 from vearutop/streaming-decode

Wait for io.EOF when decoding a stream
This commit is contained in:
Masaaki Goshima 2021-02-10 15:32:50 +09:00 committed by GitHub
commit d7c9594839
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

View File

@ -75,7 +75,7 @@ func (s *stream) read() bool {
buf[last] = nul buf[last] = nul
n, err := s.r.Read(buf[:last]) n, err := s.r.Read(buf[:last])
s.length = s.cursor + int64(n) s.length = s.cursor + int64(n)
if n < last || err == io.EOF { if err == io.EOF {
s.allRead = true s.allRead = true
} else if err != nil { } else if err != nil {
return false return false

View File

@ -6,6 +6,7 @@ package json_test
import ( import (
"bytes" "bytes"
"compress/gzip"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -463,3 +464,41 @@ func TestHTTPDecoding(t *testing.T) {
t.Errorf("err = %v; want io.EOF", err) 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)
}
}