Skip to content

Add Iterator.StringReader() #359

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions iter_str_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package jsoniter

import "io"

type stringReader struct {
eof bool
iter *Iterator
}

// Read implements io.Reader. There is no provision for resetting the reader
// after EOF, nor for ensuring that the full JSON string is read to its end.
func (r *stringReader) Read(p []byte) (n int, err error) {
if r.eof {
return 0, io.EOF
}
p = p[:0]
iter := r.iter
for iter.Error == nil && len(p) < cap(p) {
c := iter.readByte()
if c == '"' {
r.eof = true
break
}
if c == '\\' {
// if we don't have room for 2 runes, leave it for the next call
if len(p)+8 > cap(p) {
iter.unreadByte()
break
}
c = iter.readByte()
p = iter.readEscapedChar(c, p)
} else {
p = append(p, c)
}
}
return len(p), nil
}

// StringReader provides an io.Reader for the upcoming JSON string value. The
// consumer absolutely MUST consume the entire string in order to preserve the
// state of the Iterator. If the next value in the JSON stream is not a string,
// the returned reader will be nil.
func (iter *Iterator) StringReader() io.Reader {
c := iter.nextToken()
if c == '"' {
return &stringReader{iter: iter}
} else if c == 'n' {
iter.skipThreeBytes('u', 'l', 'l')
return nil
}
iter.ReportError("StringReader", `expects " or n, but found `+string([]byte{c}))
return nil
}