1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-30 08:36:43 +02:00
json-iterator/feature_iter_object.go

196 lines
4.0 KiB
Go
Raw Normal View History

package jsoniter
import (
"fmt"
"unicode"
2017-06-17 16:42:11 +02:00
"unsafe"
)
func (iter *Iterator) ReadObject() (ret string) {
c := iter.nextToken()
switch c {
case 'n':
2017-01-18 17:33:40 +02:00
iter.skipFixedBytes(3)
return "" // null
case '{':
c = iter.nextToken()
2017-01-20 06:56:49 +02:00
if c == '"' {
iter.unreadByte()
2017-01-20 06:40:52 +02:00
return string(iter.readObjectFieldAsBytes())
}
2017-01-20 06:56:49 +02:00
if c == '}' {
return "" // end of object
}
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadObject", `expect " after {`)
2017-01-20 06:56:49 +02:00
return
case ',':
2017-01-20 06:40:52 +02:00
return string(iter.readObjectFieldAsBytes())
case '}':
return "" // end of object
default:
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadObject", fmt.Sprintf(`expect { or , or } or n, but found %s`, string([]byte{c})))
return
}
}
2017-02-08 07:46:28 +02:00
func (iter *Iterator) readFieldHash() int32 {
2017-02-09 07:35:58 +02:00
hash := int64(0x811c9dc5)
2017-02-08 07:46:28 +02:00
c := iter.nextToken()
if c == '"' {
for {
for i := iter.head; i < iter.tail; i++ {
// require ascii string and no escape
b := iter.buf[i]
if 'A' <= b && b <= 'Z' {
b += 'a' - 'A'
}
2017-02-08 07:46:28 +02:00
if b == '"' {
2017-06-06 17:27:00 +02:00
iter.head = i + 1
2017-02-08 07:46:28 +02:00
c = iter.nextToken()
if c != ':' {
2017-06-20 09:11:01 +02:00
iter.ReportError("readFieldHash", `expect :, but found `+string([]byte{c}))
2017-02-08 07:46:28 +02:00
}
return int32(hash)
}
2017-02-09 07:50:06 +02:00
hash ^= int64(b)
2017-02-08 07:46:28 +02:00
hash *= 0x1000193
}
if !iter.loadMore() {
2017-06-20 09:11:01 +02:00
iter.ReportError("readFieldHash", `incomplete field name`)
2017-02-08 07:46:28 +02:00
return 0
}
}
}
2017-06-20 09:11:01 +02:00
iter.ReportError("readFieldHash", `expect ", but found `+string([]byte{c}))
2017-02-08 07:46:28 +02:00
return 0
}
func calcHash(str string) int32 {
2017-02-09 07:35:58 +02:00
hash := int64(0x811c9dc5)
2017-02-08 07:46:28 +02:00
for _, b := range str {
hash ^= int64(unicode.ToLower(b))
2017-02-08 07:46:28 +02:00
hash *= 0x1000193
}
return int32(hash)
}
2017-01-20 06:56:49 +02:00
func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
c := iter.nextToken()
if c == '{' {
c = iter.nextToken()
if c == '"' {
iter.unreadByte()
2017-06-17 16:42:11 +02:00
field := iter.readObjectFieldAsBytes()
if !callback(iter, *(*string)(unsafe.Pointer(&field))) {
2017-01-20 06:56:49 +02:00
return false
}
2017-01-20 07:54:51 +02:00
for iter.nextToken() == ',' {
2017-06-17 16:42:11 +02:00
field = iter.readObjectFieldAsBytes()
if !callback(iter, *(*string)(unsafe.Pointer(&field))) {
2017-01-20 06:56:49 +02:00
return false
}
}
return true
}
if c == '}' {
return true
}
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadObjectCB", `expect " after }`)
2017-01-20 06:56:49 +02:00
return false
}
if c == 'n' {
iter.skipFixedBytes(3)
return true // null
}
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadObjectCB", `expect { or n`)
2017-01-20 06:56:49 +02:00
return false
}
func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
c := iter.nextToken()
if c == '{' {
c = iter.nextToken()
if c == '"' {
iter.unreadByte()
field := iter.ReadString()
if iter.nextToken() != ':' {
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadMapCB", "expect : after object field")
return false
}
if !callback(iter, field) {
return false
}
for iter.nextToken() == ',' {
field = iter.ReadString()
if iter.nextToken() != ':' {
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadMapCB", "expect : after object field")
return false
}
if !callback(iter, field) {
return false
}
}
return true
}
if c == '}' {
return true
}
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadMapCB", `expect " after }`)
return false
}
if c == 'n' {
iter.skipFixedBytes(3)
return true // null
}
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadMapCB", `expect { or n`)
return false
}
func (iter *Iterator) readObjectStart() bool {
c := iter.nextToken()
if c == '{' {
c = iter.nextToken()
if c == '}' {
return false
}
iter.unreadByte()
return true
2017-05-23 12:46:11 +02:00
} else if c == 'n' {
iter.skipFixedBytes(3)
return false
}
2017-06-20 09:11:01 +02:00
iter.ReportError("readObjectStart", "expect { or n")
return false
}
2017-01-20 06:40:52 +02:00
func (iter *Iterator) readObjectFieldAsBytes() (ret []byte) {
str := iter.ReadStringAsSlice()
if iter.skipWhitespacesWithoutLoadMore() {
2017-01-20 06:40:52 +02:00
if ret == nil {
ret = make([]byte, len(str))
copy(ret, str)
}
if !iter.loadMore() {
return
}
}
if iter.buf[iter.head] != ':' {
2017-06-20 09:11:01 +02:00
iter.ReportError("readObjectFieldAsBytes", "expect : after object field")
return
}
iter.head++
if iter.skipWhitespacesWithoutLoadMore() {
2017-01-20 06:40:52 +02:00
if ret == nil {
ret = make([]byte, len(str))
copy(ret, str)
}
if !iter.loadMore() {
return
}
}
2017-01-20 06:40:52 +02:00
if ret == nil {
return str
}
return ret
}