Skip to content

Commit

Permalink
Merge pull request #36 from tdakkota/fix/keep-head-non-negative
Browse files Browse the repository at this point in the history
fix: do not unread, if next was failed
  • Loading branch information
ernado committed Feb 22, 2022
2 parents 72da542 + 0815fe3 commit 98f3014
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions dec_read.go
Expand Up @@ -7,8 +7,10 @@ import (

// Next gets Type of relatively next json element
func (d *Decoder) Next() Type {
v, _ := d.next()
d.unread()
v, err := d.next()
if err == nil {
d.unread()
}
return types[v]
}

Expand Down
11 changes: 11 additions & 0 deletions dec_read_test.go
@@ -1,6 +1,7 @@
package jx

import (
"io"
"strings"
"testing"

Expand All @@ -19,3 +20,13 @@ func TestDecoder_consume(t *testing.T) {
d := Decode(r, 1)
require.ErrorIs(t, d.consume('"'), r.Err())
}

func TestDecoder_Next(t *testing.T) {
d := DecodeBytes(nil)
d.Next()
d.Next()
d.Next()

_, err := d.Str()
require.ErrorIs(t, err, io.ErrUnexpectedEOF)
}

0 comments on commit 98f3014

Please sign in to comment.