Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not unread, if next was failed #36

Merged
merged 1 commit into from Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}