go-json/decode_stream.go

198 lines
3.4 KiB
Go
Raw Normal View History

2020-05-24 15:31:10 +03:00
package json
import (
"bytes"
"io"
)
const (
2020-12-05 16:27:33 +03:00
initBufSize = 512
2020-05-24 15:31:10 +03:00
)
type stream struct {
2020-08-14 11:59:49 +03:00
buf []byte
2020-12-05 16:27:33 +03:00
bufSize int64
2020-08-14 11:59:49 +03:00
length int64
r io.Reader
offset int64
cursor int64
2020-12-05 16:27:33 +03:00
readPos int64
2020-08-14 11:59:49 +03:00
allRead bool
useNumber bool
disallowUnknownFields bool
2020-05-24 15:31:10 +03:00
}
2020-12-05 16:27:33 +03:00
func newStream(r io.Reader) *stream {
return &stream{
r: r,
bufSize: initBufSize,
}
}
2020-05-24 15:31:10 +03:00
func (s *stream) buffered() io.Reader {
return bytes.NewReader(s.buf[s.cursor:])
}
func (s *stream) totalOffset() int64 {
return s.offset + s.cursor
}
2020-07-30 16:41:53 +03:00
func (s *stream) prevChar() byte {
return s.buf[s.cursor-1]
}
2020-05-24 15:31:10 +03:00
func (s *stream) char() byte {
return s.buf[s.cursor]
}
2020-07-30 16:41:53 +03:00
func (s *stream) reset() {
2020-12-05 16:27:33 +03:00
s.offset += s.cursor
2020-07-30 16:41:53 +03:00
s.buf = s.buf[s.cursor:]
s.cursor = 0
2020-12-05 16:27:33 +03:00
s.length = int64(len(s.buf))
}
func (s *stream) readBuf() []byte {
s.bufSize *= 2
remainBuf := s.buf
s.buf = make([]byte, s.bufSize)
copy(s.buf, remainBuf)
return s.buf[s.cursor:]
2020-07-30 16:41:53 +03:00
}
2020-05-24 15:31:10 +03:00
func (s *stream) read() bool {
2020-07-31 06:22:00 +03:00
if s.allRead {
return false
}
2020-12-05 16:27:33 +03:00
buf := s.readBuf()
last := len(buf) - 1
buf[last] = nul
n, err := s.r.Read(buf[:last])
s.length = s.cursor + int64(n)
if n < last || err == io.EOF {
2020-07-30 16:41:53 +03:00
s.allRead = true
2020-12-05 16:27:33 +03:00
} else if err != nil {
2020-07-30 16:41:53 +03:00
return false
}
2020-05-24 15:31:10 +03:00
return true
}
2020-07-30 16:41:53 +03:00
func (s *stream) skipWhiteSpace() {
LOOP:
c := s.char()
if isWhiteSpace[c] {
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
goto LOOP
} else if c == nul {
2020-07-31 11:10:03 +03:00
if s.read() {
goto LOOP
}
2020-07-30 16:41:53 +03:00
}
}
func (s *stream) skipValue() error {
s.skipWhiteSpace()
braceCount := 0
bracketCount := 0
2020-11-27 11:41:47 +03:00
start := s.cursor
2020-07-30 16:41:53 +03:00
for {
switch s.char() {
2020-07-31 11:10:03 +03:00
case nul:
if s.read() {
continue
}
2020-11-27 11:41:47 +03:00
if start == s.cursor {
return errUnexpectedEndOfJSON("value of object", s.totalOffset())
}
if braceCount == 0 && bracketCount == 0 {
return nil
}
2020-07-31 11:10:03 +03:00
return errUnexpectedEndOfJSON("value of object", s.totalOffset())
2020-07-30 16:41:53 +03:00
case '{':
braceCount++
case '[':
bracketCount++
case '}':
braceCount--
if braceCount == -1 && bracketCount == 0 {
return nil
}
case ']':
bracketCount--
2020-11-27 11:41:47 +03:00
if braceCount == 0 && bracketCount == -1 {
2020-11-27 11:11:53 +03:00
return nil
}
2020-07-30 16:41:53 +03:00
case ',':
if bracketCount == 0 && braceCount == 0 {
return nil
}
case '"':
2020-07-31 11:10:03 +03:00
for {
s.cursor++
c := s.char()
if c == nul {
2020-07-31 11:10:03 +03:00
if !s.read() {
return errUnexpectedEndOfJSON("value of string", s.totalOffset())
}
c = s.char()
2020-07-31 11:10:03 +03:00
}
if c != '"' {
2020-07-30 16:41:53 +03:00
continue
}
if s.prevChar() == '\\' {
continue
}
if bracketCount == 0 && braceCount == 0 {
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
return nil
}
break
}
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
2020-07-31 11:10:03 +03:00
for {
s.cursor++
c := s.char()
if floatTable[c] {
2020-07-30 16:41:53 +03:00
continue
} else if c == nul {
2020-07-31 11:10:03 +03:00
if s.read() {
s.cursor-- // for retry current character
2020-07-31 11:10:03 +03:00
continue
}
2020-07-30 16:41:53 +03:00
}
break
}
if bracketCount == 0 && braceCount == 0 {
return nil
}
continue
2020-07-31 14:24:39 +03:00
case 't':
if err := trueBytes(s); err != nil {
return err
}
if bracketCount == 0 && braceCount == 0 {
return nil
}
continue
case 'f':
if err := falseBytes(s); err != nil {
return err
}
if bracketCount == 0 && braceCount == 0 {
return nil
}
continue
case 'n':
if err := nullBytes(s); err != nil {
return err
}
if bracketCount == 0 && braceCount == 0 {
return nil
}
continue
2020-07-30 16:41:53 +03:00
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
}
return errUnexpectedEndOfJSON("value of object", s.offset)
}