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

85 lines
1.6 KiB
Go
Raw Normal View History

package jsoniter
// ReadObject is a implemented iterator for json
func (iter *Iterator) ReadObject() (ret string) {
c := iter.nextToken()
if iter.Error != nil {
return
}
switch c {
case 'n':
2017-01-18 17:33:40 +02:00
iter.skipFixedBytes(3)
if iter.Error != nil {
return
}
return "" // null
case '{':
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())
default:
iter.reportError("ReadObject", `expect " after {`)
return
}
case ',':
2017-01-20 06:40:52 +02:00
return string(iter.readObjectFieldAsBytes())
case '}':
return "" // end of object
default:
iter.reportError("ReadObject", `expect { or , or } or n`)
return
}
}
func (iter *Iterator) readObjectStart() bool {
c := iter.nextToken()
if c == '{' {
c = iter.nextToken()
if c == '}' {
return false
}
iter.unreadByte()
return true
}
iter.reportError("readObjectStart", "expect { ")
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-01-20 06:40:52 +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
}