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

read object cb

This commit is contained in:
Tao Wen 2017-01-20 12:56:49 +08:00
parent d14b025931
commit 80c86e63b1
2 changed files with 55 additions and 31 deletions

View File

@ -1,33 +1,22 @@
package jsoniter package jsoniter
// ReadObject is a implemented iterator for json
func (iter *Iterator) ReadObject() (ret string) { func (iter *Iterator) ReadObject() (ret string) {
c := iter.nextToken() c := iter.nextToken()
if iter.Error != nil {
return
}
switch c { switch c {
case 'n': case 'n':
iter.skipFixedBytes(3) iter.skipFixedBytes(3)
if iter.Error != nil {
return
}
return "" // null return "" // null
case '{': case '{':
c = iter.nextToken() c = iter.nextToken()
if iter.Error != nil { if c == '"' {
return
}
switch c {
case '}':
return "" // end of object
case '"':
iter.unreadByte() iter.unreadByte()
return string(iter.readObjectFieldAsBytes()) return string(iter.readObjectFieldAsBytes())
default:
iter.reportError("ReadObject", `expect " after {`)
return
} }
if c == '}' {
return "" // end of object
}
iter.reportError("ReadObject", `expect " after {`)
return
case ',': case ',':
return string(iter.readObjectFieldAsBytes()) return string(iter.readObjectFieldAsBytes())
case '}': case '}':
@ -38,6 +27,39 @@ func (iter *Iterator) ReadObject() (ret string) {
} }
} }
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
}
c = iter.nextToken()
for c == ',' {
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
}
func (iter *Iterator) readObjectStart() bool { func (iter *Iterator) readObjectStart() bool {
c := iter.nextToken() c := iter.nextToken()
if c == '{' { if c == '{' {

View File

@ -2,35 +2,37 @@ package jsoniter
import ( import (
"encoding/json" "encoding/json"
"fmt"
"testing" "testing"
"github.com/json-iterator/go/require" "github.com/json-iterator/go/require"
"bytes" "bytes"
) )
func Test_empty_object(t *testing.T) { func Test_empty_object(t *testing.T) {
should := require.New(t)
iter := ParseString(`{}`) iter := ParseString(`{}`)
field := iter.ReadObject() field := iter.ReadObject()
if field != "" { should.Equal("", field)
t.Fatal(field) iter = ParseString(`{}`)
} iter.ReadObjectCB(func(iter *Iterator, field string) bool {
should.FailNow("should not call")
return true
})
} }
func Test_one_field(t *testing.T) { func Test_one_field(t *testing.T) {
should := require.New(t)
iter := ParseString(`{"a": "b"}`) iter := ParseString(`{"a": "b"}`)
field := iter.ReadObject() field := iter.ReadObject()
if field != "a" { should.Equal("a", field)
fmt.Println(iter.Error)
t.Fatal(field)
}
value := iter.ReadString() value := iter.ReadString()
if value != "b" { should.Equal("b", value)
t.Fatal(field)
}
field = iter.ReadObject() field = iter.ReadObject()
if field != "" { should.Equal("", field)
t.Fatal(field) iter = ParseString(`{"a": "b"}`)
} should.True(iter.ReadObjectCB(func(iter *Iterator, field string) bool {
should.Equal("a", field)
return true
}))
} }
func Test_two_field(t *testing.T) { func Test_two_field(t *testing.T) {