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

350 lines
7.7 KiB
Go
Raw Normal View History

2016-11-30 18:56:25 +02:00
package jsoniter
import (
2017-07-11 18:11:50 +02:00
"encoding/json"
2016-11-30 18:56:25 +02:00
"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 (
// InvalidValue invalid JSON element
InvalidValue ValueType = iota
// StringValue JSON element "string"
StringValue
// NumberValue JSON element 100 or 0.10
NumberValue
// NilValue JSON element null
NilValue
// BoolValue JSON element true or false
BoolValue
// ArrayValue JSON element []
ArrayValue
// ObjectValue JSON element {}
ObjectValue
2016-12-10 08:34:36 +02:00
)
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] = InvalidValue
2016-12-10 08:34:36 +02:00
}
valueTypes['"'] = StringValue
valueTypes['-'] = NumberValue
valueTypes['0'] = NumberValue
valueTypes['1'] = NumberValue
valueTypes['2'] = NumberValue
valueTypes['3'] = NumberValue
valueTypes['4'] = NumberValue
valueTypes['5'] = NumberValue
valueTypes['6'] = NumberValue
valueTypes['7'] = NumberValue
valueTypes['8'] = NumberValue
valueTypes['9'] = NumberValue
valueTypes['t'] = BoolValue
valueTypes['f'] = BoolValue
valueTypes['n'] = NilValue
valueTypes['['] = ArrayValue
valueTypes['{'] = ObjectValue
}
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
2019-10-08 17:17:01 +02:00
depth int
captureStartedAt int
2017-06-19 17:43:53 +02:00
captured []byte
Error error
Attachment interface{} // open for customized decoder
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,
2019-10-08 17:17:01 +02:00
depth: 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,
2019-10-08 17:17:01 +02:00
depth: 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),
2019-10-08 17:17:01 +02:00
depth: 0,
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
2019-10-08 17:17:01 +02:00
iter.depth = 0
2016-12-10 08:34:36 +02:00
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)
2019-10-08 17:17:01 +02:00
iter.depth = 0
2016-12-06 07:48:03 +02:00
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, unexpected char "+string([]byte{c}))
2017-07-06 10:04:52 +02:00
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
}
2017-10-09 02:16:52 +02:00
peekEnd := iter.head + 10
if peekEnd > iter.tail {
peekEnd = iter.tail
}
parsing := string(iter.buf[peekStart:peekEnd])
contextStart := iter.head - 50
if contextStart < 0 {
contextStart = 0
}
contextEnd := iter.head + 50
if contextEnd > iter.tail {
contextEnd = iter.tail
}
context := string(iter.buf[contextStart:contextEnd])
iter.Error = fmt.Errorf("%s: %s, error found in #%v byte of ...|%s|..., bigger context ...|%s|...",
2017-10-10 02:57:02 +02:00
operation, msg, iter.head-peekStart, parsing, context)
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
}
2017-10-09 02:16:52 +02:00
return fmt.Sprintf("parsing #%v byte, around ...|%s|..., whole buffer ...|%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
}
2017-07-12 11:56:51 +02:00
if iter.captured != nil {
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() {
2017-07-18 03:45:25 +02:00
if iter.Error != nil {
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 StringValue:
2017-01-21 10:09:38 +02:00
return iter.ReadString()
case NumberValue:
if iter.cfg.configBeforeFrozen.UseNumber {
2017-07-11 18:23:49 +02:00
return json.Number(iter.readNumberAsString())
}
2017-01-21 10:09:38 +02:00
return iter.ReadFloat64()
case NilValue:
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 BoolValue:
2017-01-21 10:09:38 +02:00
return iter.ReadBool()
case ArrayValue:
2017-01-21 10:09:38 +02:00
arr := []interface{}{}
iter.ReadArrayCB(func(iter *Iterator) bool {
var elem interface{}
iter.ReadVal(&elem)
arr = append(arr, elem)
2017-01-21 10:09:38 +02:00
return true
})
return arr
case ObjectValue:
2017-01-21 10:09:38 +02:00
obj := map[string]interface{}{}
iter.ReadMapCB(func(Iter *Iterator, field string) bool {
var elem interface{}
iter.ReadVal(&elem)
obj[field] = elem
2017-01-21 10:09:38 +02:00
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
}
}
2019-10-08 17:17:01 +02:00
// limit maximum depth of nesting, as allowed by https://tools.ietf.org/html/rfc7159#section-9
const maxDepth = 10000
2019-10-08 17:17:01 +02:00
func (iter *Iterator) incrementDepth() (success bool) {
iter.depth++
if iter.depth <= maxDepth {
2019-10-08 17:17:01 +02:00
return true
}
iter.ReportError("incrementDepth", "exceeded max depth")
return false
}
func (iter *Iterator) decrementDepth() (success bool) {
iter.depth--
if iter.depth >= 0 {
return true
}
iter.ReportError("decrementDepth", "unexpected negative nesting")
return false
}