Skip to content

Commit

Permalink
Fix stream decoding with error
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Oct 27, 2021
1 parent a89c9e3 commit 8f8d2d8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions decode.go
Expand Up @@ -200,6 +200,7 @@ func (d *Decoder) DecodeWithOption(v interface{}, optFuncs ...DecodeOptionFunc)
optFunc(s.Option)
}
if err := dec.DecodeStream(s, 0, header.ptr); err != nil {
d.s.SkipErrorValue()
return err
}
s.Reset()
Expand Down
15 changes: 15 additions & 0 deletions decode_test.go
Expand Up @@ -3776,3 +3776,18 @@ func TestIssue282(t *testing.T) {
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")
}
}
4 changes: 4 additions & 0 deletions internal/decoder/stream.go
Expand Up @@ -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 {
_, cursor, p := s.stat()
for {
Expand Down

0 comments on commit 8f8d2d8

Please sign in to comment.