Fix stream decoding with error

This commit is contained in:
Masaaki Goshima 2021-10-27 22:54:34 +09:00
parent a89c9e30df
commit 8f8d2d8cea
No known key found for this signature in database
GPG Key ID: 6A53785055537153
3 changed files with 20 additions and 0 deletions

View File

@ -200,6 +200,7 @@ func (d *Decoder) DecodeWithOption(v interface{}, optFuncs ...DecodeOptionFunc)
optFunc(s.Option) optFunc(s.Option)
} }
if err := dec.DecodeStream(s, 0, header.ptr); err != nil { if err := dec.DecodeStream(s, 0, header.ptr); err != nil {
d.s.SkipErrorValue()
return err return err
} }
s.Reset() s.Reset()

View File

@ -3776,3 +3776,18 @@ func TestIssue282(t *testing.T) {
t.Fatalf("failed to assign map value") t.Fatalf("failed to assign map value")
} }
} }
func TestDecodeStreamWithError(t *testing.T) {
type Test struct {
Val int `json:"val"`
}
p := Test{}
dec := json.NewDecoder(strings.NewReader("[12,{\"val\" : 2}]"))
dec.Token()
for dec.More() {
_ = dec.Decode(&p) // ignore error value
}
if p.Val != 2 {
t.Fatal("failed to decode")
}
}

View File

@ -369,6 +369,10 @@ func (s *Stream) skipArray(depth int64) error {
} }
} }
func (s *Stream) SkipErrorValue() {
_ = s.skipValue(0)
}
func (s *Stream) skipValue(depth int64) error { func (s *Stream) skipValue(depth int64) error {
_, cursor, p := s.stat() _, cursor, p := s.stat()
for { for {