Skip to content

Commit 8f8d2d8

Browse files
committed
Fix stream decoding with error
1 parent a89c9e3 commit 8f8d2d8

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

decode.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ func (d *Decoder) DecodeWithOption(v interface{}, optFuncs ...DecodeOptionFunc)
200200
optFunc(s.Option)
201201
}
202202
if err := dec.DecodeStream(s, 0, header.ptr); err != nil {
203+
d.s.SkipErrorValue()
203204
return err
204205
}
205206
s.Reset()

decode_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3776,3 +3776,18 @@ func TestIssue282(t *testing.T) {
37763776
t.Fatalf("failed to assign map value")
37773777
}
37783778
}
3779+
3780+
func TestDecodeStreamWithError(t *testing.T) {
3781+
type Test struct {
3782+
Val int `json:"val"`
3783+
}
3784+
p := Test{}
3785+
dec := json.NewDecoder(strings.NewReader("[12,{\"val\" : 2}]"))
3786+
dec.Token()
3787+
for dec.More() {
3788+
_ = dec.Decode(&p) // ignore error value
3789+
}
3790+
if p.Val != 2 {
3791+
t.Fatal("failed to decode")
3792+
}
3793+
}

internal/decoder/stream.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ func (s *Stream) skipArray(depth int64) error {
369369
}
370370
}
371371

372+
func (s *Stream) SkipErrorValue() {
373+
_ = s.skipValue(0)
374+
}
375+
372376
func (s *Stream) skipValue(depth int64) error {
373377
_, cursor, p := s.stat()
374378
for {

0 commit comments

Comments
 (0)