2017-01-05 07:53:38 +02:00
|
|
|
package jsoniter
|
|
|
|
|
2017-01-05 15:23:08 +02:00
|
|
|
// ReadObject is a implemented iterator for json
|
2017-01-05 07:53:38 +02:00
|
|
|
func (iter *Iterator) ReadObject() (ret string) {
|
|
|
|
c := iter.nextToken()
|
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return "" // null
|
2017-01-05 15:23:08 +02:00
|
|
|
case '{':
|
2017-01-05 07:53:38 +02:00
|
|
|
c = iter.nextToken()
|
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch c {
|
|
|
|
case '}':
|
|
|
|
return "" // end of object
|
|
|
|
case '"':
|
|
|
|
iter.unreadByte()
|
2017-01-20 06:40:52 +02:00
|
|
|
return string(iter.readObjectFieldAsBytes())
|
2017-01-05 07:53:38 +02:00
|
|
|
default:
|
2017-01-05 15:23:08 +02:00
|
|
|
iter.reportError("ReadObject", `expect " after {`)
|
2017-01-05 07:53:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|