2017-01-05 07:53:38 +02:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
func (iter *Iterator) ReadObject() (ret string) {
|
|
|
|
c := iter.nextToken()
|
|
|
|
switch c {
|
2017-01-05 15:23:08 +02:00
|
|
|
case 'n':
|
2017-01-18 17:33:40 +02:00
|
|
|
iter.skipFixedBytes(3)
|
2017-01-05 07:53:38 +02:00
|
|
|
return "" // null
|
2017-01-05 15:23:08 +02:00
|
|
|
case '{':
|
2017-01-05 07:53:38 +02:00
|
|
|
c = iter.nextToken()
|
2017-01-20 06:56:49 +02:00
|
|
|
if c == '"' {
|
2017-01-05 07:53:38 +02:00
|
|
|
iter.unreadByte()
|
2017-01-20 06:40:52 +02:00
|
|
|
return string(iter.readObjectFieldAsBytes())
|
2017-01-05 07:53:38 +02:00
|
|
|
}
|
2017-01-20 06:56:49 +02:00
|
|
|
if c == '}' {
|
|
|
|
return "" // end of object
|
|
|
|
}
|
|
|
|
iter.reportError("ReadObject", `expect " after {`)
|
|
|
|
return
|
2017-01-05 07:53:38 +02:00
|
|
|
case ',':
|
2017-01-20 06:40:52 +02:00
|
|
|
return string(iter.readObjectFieldAsBytes())
|
2017-01-05 07:53:38 +02:00
|
|
|
case '}':
|
|
|
|
return "" // end of object
|
|
|
|
default:
|
2017-01-05 15:23:08 +02:00
|
|
|
iter.reportError("ReadObject", `expect { or , or } or n`)
|
2017-01-05 07:53:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
field := string(iter.readObjectFieldAsBytes())
|
|
|
|
if !callback(iter, field) {
|
|
|
|
return false
|
|
|
|
}
|
2017-01-20 07:54:51 +02:00
|
|
|
for iter.nextToken() == ',' {
|
2017-01-20 06:56:49 +02:00
|
|
|
field := string(iter.readObjectFieldAsBytes())
|
|
|
|
if !callback(iter, field) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if c == '}' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
iter.reportError("ReadObjectCB", `expect " after }`)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if c == 'n' {
|
|
|
|
iter.skipFixedBytes(3)
|
|
|
|
return true // null
|
|
|
|
}
|
|
|
|
iter.reportError("ReadObjectCB", `expect { or n`)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-01-05 07:53:38 +02:00
|
|
|
func (iter *Iterator) readObjectStart() bool {
|
|
|
|
c := iter.nextToken()
|
|
|
|
if c == '{' {
|
|
|
|
c = iter.nextToken()
|
|
|
|
if c == '}' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
iter.unreadByte()
|
|
|
|
return true
|
|
|
|
}
|
2017-01-05 15:23:08 +02:00
|
|
|
iter.reportError("readObjectStart", "expect { ")
|
2017-01-05 07:53:38 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-01-20 06:40:52 +02:00
|
|
|
func (iter *Iterator) readObjectFieldAsBytes() (ret []byte) {
|
2017-01-16 17:57:09 +02:00
|
|
|
str := iter.ReadStringAsSlice()
|
2017-01-05 07:53:38 +02:00
|
|
|
if iter.skipWhitespacesWithoutLoadMore() {
|
2017-01-20 06:40:52 +02:00
|
|
|
if ret == nil {
|
|
|
|
ret = make([]byte, len(str))
|
|
|
|
copy(ret, str)
|
2017-01-05 07:53:38 +02:00
|
|
|
}
|
|
|
|
if !iter.loadMore() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if iter.buf[iter.head] != ':' {
|
2017-01-20 06:40:52 +02:00
|
|
|
iter.reportError("readObjectFieldAsBytes", "expect : after object field")
|
2017-01-05 07:53:38 +02:00
|
|
|
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)
|
2017-01-05 07:53:38 +02:00
|
|
|
}
|
|
|
|
if !iter.loadMore() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2017-01-20 06:40:52 +02:00
|
|
|
if ret == nil {
|
|
|
|
return str
|
2017-01-05 07:53:38 +02:00
|
|
|
}
|
|
|
|
return ret
|
2017-01-05 15:23:08 +02:00
|
|
|
}
|