1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-24 08:22:14 +02:00
json-iterator/feature_iter.go

301 lines
6.4 KiB
Go
Raw Normal View History

2016-11-30 18:56:25 +02:00
package jsoniter
import (
"fmt"
"io"
2016-11-30 18:56:25 +02:00
)
2017-07-09 10:09:23 +02:00
// ValueType the type for JSON element
2016-12-10 08:34:36 +02:00
type ValueType int
const (
2017-07-09 10:09:23 +02:00
// Invalid invalid JSON element
2016-12-10 08:34:36 +02:00
Invalid ValueType = iota
2017-07-09 10:09:23 +02:00
// String JSON element "string"
2016-12-10 08:34:36 +02:00
String
2017-07-09 10:09:23 +02:00
// Number JSON element 100 or 0.10
2016-12-10 08:34:36 +02:00
Number
2017-07-09 10:09:23 +02:00
// Nil JSON element null
2017-01-21 10:09:38 +02:00
Nil
2017-07-09 10:09:23 +02:00
// Bool JSON element true or false
2016-12-11 04:04:26 +02:00
Bool
2017-07-09 10:09:23 +02:00
// Array JSON element []
2016-12-10 08:34:36 +02:00
Array
2017-07-09 10:09:23 +02:00
// Object JSON element {}
2016-12-10 08:34:36 +02:00
Object
)
2017-01-15 16:50:31 +02:00
var hexDigits []byte
2016-12-10 08:34:36 +02:00
var valueTypes []ValueType
2016-12-06 04:41:05 +02:00
func init() {
2017-01-15 16:50:31 +02:00
hexDigits = make([]byte, 256)
for i := 0; i < len(hexDigits); i++ {
hexDigits[i] = 255
2016-12-06 04:41:05 +02:00
}
for i := '0'; i <= '9'; i++ {
2017-01-15 16:50:31 +02:00
hexDigits[i] = byte(i - '0')
2016-12-06 04:41:05 +02:00
}
for i := 'a'; i <= 'f'; i++ {
2017-01-15 16:50:31 +02:00
hexDigits[i] = byte((i - 'a') + 10)
2016-12-06 04:41:05 +02:00
}
for i := 'A'; i <= 'F'; i++ {
2017-01-15 16:50:31 +02:00
hexDigits[i] = byte((i - 'A') + 10)
2016-12-06 04:41:05 +02:00
}
2016-12-10 08:34:36 +02:00
valueTypes = make([]ValueType, 256)
for i := 0; i < len(valueTypes); i++ {
valueTypes[i] = Invalid
}
valueTypes['"'] = String
valueTypes['-'] = Number
valueTypes['0'] = Number
valueTypes['1'] = Number
valueTypes['2'] = Number
valueTypes['3'] = Number
valueTypes['4'] = Number
valueTypes['5'] = Number
valueTypes['6'] = Number
valueTypes['7'] = Number
valueTypes['8'] = Number
valueTypes['9'] = Number
valueTypes['t'] = Bool
valueTypes['f'] = Bool
2017-01-21 10:09:38 +02:00
valueTypes['n'] = Nil
valueTypes['['] = Array
valueTypes['{'] = Object
}
2017-07-09 10:09:23 +02:00
// Iterator is a io.Reader like object, with JSON specific read functions.
// Error is not returned as return value, but stored as Error member on this iterator instance.
2016-11-30 18:56:25 +02:00
type Iterator struct {
2017-06-19 17:43:53 +02:00
cfg *frozenConfig
reader io.Reader
buf []byte
head int
tail int
captureStartedAt int
2017-06-19 17:43:53 +02:00
captured []byte
Error error
2016-11-30 18:56:25 +02:00
}
2017-07-09 10:09:23 +02:00
// NewIterator creates an empty Iterator instance
func NewIterator(cfg API) *Iterator {
return &Iterator{
2017-07-09 10:09:23 +02:00
cfg: cfg.(*frozenConfig),
reader: nil,
buf: nil,
head: 0,
tail: 0,
}
}
2017-07-09 10:09:23 +02:00
// Parse creates an Iterator instance from io.Reader
func Parse(cfg API, reader io.Reader, bufSize int) *Iterator {
return &Iterator{
2017-07-09 10:09:23 +02:00
cfg: cfg.(*frozenConfig),
2016-12-01 04:35:38 +02:00
reader: reader,
buf: make([]byte, bufSize),
head: 0,
tail: 0,
2016-11-30 18:56:25 +02:00
}
}
2017-07-09 10:09:23 +02:00
// ParseBytes creates an Iterator instance from byte array
func ParseBytes(cfg API, input []byte) *Iterator {
return &Iterator{
2017-07-09 10:09:23 +02:00
cfg: cfg.(*frozenConfig),
2016-12-01 04:35:38 +02:00
reader: nil,
buf: input,
head: 0,
tail: len(input),
2016-11-30 18:56:25 +02:00
}
}
2017-07-09 10:09:23 +02:00
// ParseString creates an Iterator instance from string
func ParseString(cfg API, input string) *Iterator {
2017-06-13 10:58:53 +02:00
return ParseBytes(cfg, []byte(input))
2016-12-10 08:34:36 +02:00
}
2017-07-09 10:09:23 +02:00
// Pool returns a pool can provide more iterator with same configuration
func (iter *Iterator) Pool() IteratorPool {
2017-06-20 09:18:24 +02:00
return iter.cfg
}
2017-07-09 10:09:23 +02:00
// Reset reuse iterator instance by specifying another reader
2016-12-10 08:34:36 +02:00
func (iter *Iterator) Reset(reader io.Reader) *Iterator {
iter.reader = reader
iter.head = 0
iter.tail = 0
return iter
}
2017-07-09 10:09:23 +02:00
// ResetBytes reuse iterator instance by specifying another byte array as input
2016-12-10 08:34:36 +02:00
func (iter *Iterator) ResetBytes(input []byte) *Iterator {
2016-12-06 07:48:03 +02:00
iter.reader = nil
iter.buf = input
iter.head = 0
iter.tail = len(input)
return iter
}
2017-07-09 10:09:23 +02:00
// WhatIsNext gets ValueType of relatively next json element
2016-12-10 08:34:36 +02:00
func (iter *Iterator) WhatIsNext() ValueType {
valueType := valueTypes[iter.nextToken()]
iter.unreadByte()
return valueType
2016-11-30 18:56:25 +02:00
}
2016-12-09 07:08:14 +02:00
func (iter *Iterator) skipWhitespacesWithoutLoadMore() bool {
for i := iter.head; i < iter.tail; i++ {
c := iter.buf[i]
switch c {
case ' ', '\n', '\t', '\r':
continue
}
iter.head = i
return false
}
return true
}
2017-07-06 10:04:52 +02:00
func (iter *Iterator) isObjectEnd() bool {
c := iter.nextToken()
if c == ',' {
return false
}
if c == '}' {
return true
}
iter.ReportError("isObjectEnd", "object ended prematurely")
return true
}
2016-12-06 14:09:19 +02:00
func (iter *Iterator) nextToken() byte {
2016-12-06 17:51:29 +02:00
// a variation of skip whitespaces, returning the next non-whitespace token
2016-12-06 14:09:19 +02:00
for {
for i := iter.head; i < iter.tail; i++ {
c := iter.buf[i]
switch c {
2016-12-15 18:25:35 +02:00
case ' ', '\n', '\t', '\r':
2016-12-06 14:09:19 +02:00
continue
}
iter.head = i + 1
2016-12-06 14:09:19 +02:00
return c
}
if !iter.loadMore() {
return 0
}
}
}
2017-07-09 10:09:23 +02:00
// ReportError record a error in iterator instance with current position.
2017-06-20 09:11:01 +02:00
func (iter *Iterator) ReportError(operation string, msg string) {
2016-12-06 05:08:36 +02:00
if iter.Error != nil {
2017-01-23 02:33:43 +02:00
if iter.Error != io.EOF {
return
}
2016-12-06 05:08:36 +02:00
}
2016-12-02 05:22:20 +02:00
peekStart := iter.head - 10
if peekStart < 0 {
peekStart = 0
}
iter.Error = fmt.Errorf("%s: %s, parsing %v ...%s... at %s", operation, msg, iter.head,
string(iter.buf[peekStart:iter.head]), string(iter.buf[0:iter.tail]))
2016-11-30 18:56:25 +02:00
}
2017-07-09 10:09:23 +02:00
// CurrentBuffer gets current buffer as string for debugging purpose
func (iter *Iterator) CurrentBuffer() string {
peekStart := iter.head - 10
if peekStart < 0 {
peekStart = 0
}
2016-12-09 07:08:14 +02:00
return fmt.Sprintf("parsing %v ...|%s|... at %s", iter.head,
string(iter.buf[peekStart:iter.head]), string(iter.buf[0:iter.tail]))
}
2016-12-01 04:35:38 +02:00
func (iter *Iterator) readByte() (ret byte) {
if iter.head == iter.tail {
2016-12-06 14:01:22 +02:00
if iter.loadMore() {
ret = iter.buf[iter.head]
iter.head++
return ret
2016-12-01 04:35:38 +02:00
}
return 0
2016-11-30 18:56:25 +02:00
}
2016-12-01 04:35:38 +02:00
ret = iter.buf[iter.head]
iter.head++
2016-12-01 04:35:38 +02:00
return ret
2016-11-30 18:56:25 +02:00
}
2016-12-06 14:01:22 +02:00
func (iter *Iterator) loadMore() bool {
if iter.reader == nil {
2017-01-24 16:36:16 +02:00
if iter.Error == nil {
2017-06-18 11:00:28 +02:00
iter.head = iter.tail
2017-01-24 16:36:16 +02:00
iter.Error = io.EOF
}
2016-12-06 14:01:22 +02:00
return false
}
if iter.captureStartedAt != -1 {
iter.captured = append(iter.captured,
iter.buf[iter.captureStartedAt:iter.tail]...)
iter.captureStartedAt = 0
}
2016-12-06 14:01:22 +02:00
for {
n, err := iter.reader.Read(iter.buf)
if n == 0 {
if err != nil {
2017-01-24 16:36:16 +02:00
if iter.Error == nil {
iter.Error = err
}
2016-12-06 14:01:22 +02:00
return false
}
} else {
iter.head = 0
iter.tail = n
return true
}
}
}
2016-12-01 04:35:38 +02:00
func (iter *Iterator) unreadByte() {
if iter.head == 0 {
2017-06-20 09:11:01 +02:00
iter.ReportError("unreadByte", "unread too many bytes")
2016-11-30 18:56:25 +02:00
return
}
iter.head--
2016-12-01 04:35:38 +02:00
return
2016-11-30 18:56:25 +02:00
}
2017-07-09 10:09:23 +02:00
// Read read the next JSON element as generic interface{}.
2017-01-21 10:09:38 +02:00
func (iter *Iterator) Read() interface{} {
valueType := iter.WhatIsNext()
switch valueType {
case String:
return iter.ReadString()
case Number:
return iter.ReadFloat64()
case Nil:
2017-07-10 09:23:35 +02:00
iter.skipFourBytes('n', 'u', 'l', 'l')
2017-01-21 10:09:38 +02:00
return nil
case Bool:
return iter.ReadBool()
case Array:
arr := []interface{}{}
iter.ReadArrayCB(func(iter *Iterator) bool {
arr = append(arr, iter.Read())
return true
})
return arr
case Object:
obj := map[string]interface{}{}
iter.ReadObjectCB(func(Iter *Iterator, field string) bool {
obj[field] = iter.Read()
return true
})
return obj
default:
2017-06-20 09:11:01 +02:00
iter.ReportError("Read", fmt.Sprintf("unexpected value type: %v", valueType))
2017-01-21 10:09:38 +02:00
return nil
}
}