1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-20 18:48:32 +02:00

support cb

This commit is contained in:
Tao Wen 2016-12-06 21:10:09 +08:00
parent c5576a8a31
commit e5e9a13f2e
2 changed files with 75 additions and 18 deletions

View File

@ -538,7 +538,7 @@ func (iter *Iterator) ReadArrayCB(cb func()) {
return // null
}
if c != '[' {
iter.ReportError("ReadArray", "expect [ or n")
iter.ReportError("ReadArrayCB", "expect [ or n")
return
}
c = iter.nextToken()
@ -557,13 +557,47 @@ func (iter *Iterator) ReadArrayCB(cb func()) {
return
}
if c != ',' {
iter.ReportError("ReadArray", "expect , or ]")
iter.ReportError("ReadArrayCB", "expect , or ]")
return
}
iter.skipWhitespaces()
}
}
func (iter *Iterator) ReadObjectCB(cb func(string)) {
c := iter.nextToken()
if c == 'n' {
iter.skipNull()
return // null
}
if c != '{' {
iter.ReportError("ReadObjectCB", "expect { or n")
return
}
c = iter.nextToken()
if c == '}' {
return // []
} else {
iter.unreadByte()
}
for {
iter.skipWhitespaces()
field := iter.readObjectField()
if iter.Error != nil {
return
}
cb(field)
c = iter.nextToken()
if c == '}' {
return // end of object
}
if c != ',' {
iter.ReportError("ReadObjectCB", `expect ,`)
return
}
}
}
func (iter *Iterator) ReadObject() (ret string) {
c := iter.nextToken()
if iter.Error != nil {
@ -607,13 +641,7 @@ func (iter *Iterator) ReadObject() (ret string) {
func (iter *Iterator) readObjectField() (ret string) {
str := iter.ReadStringAsBytes()
field := *(*string)(unsafe.Pointer(&str))
if iter.Error != nil {
return
}
c := iter.nextToken()
if iter.Error != nil {
return
}
if c != ':' {
iter.ReportError("ReadObject", "expect : after object field")
return

View File

@ -66,6 +66,18 @@ func Test_two_field(t *testing.T) {
}
}
func Test_cb(t *testing.T) {
iter := ParseString(`{ "a": "b" , "c": "d" }`)
count := 0
iter.ReadObjectCB(func(field string) {
iter.Skip()
count += 1
})
if count != 2 {
t.Fatal(count)
}
}
type TestObj struct {
Field1 string
Field2 uint64
@ -73,18 +85,35 @@ type TestObj struct {
func Benchmark_jsoniter_object(b *testing.B) {
for n := 0; n < b.N; n++ {
iter := ParseString(`{"field1": "1", "field2": 2}`)
obj := TestObj{}
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
switch field {
case "field1":
obj.Field1 = iter.ReadString()
case "field2":
obj.Field2 = iter.ReadUint64()
default:
iter.ReportError("bind object", "unexpected field")
iter := ParseString(`{"field1": "1", "field2": 2}`)
obj := TestObj{}
for field := iter.ReadObject(); field != ""; field = iter.ReadObject() {
switch field {
case "field1":
obj.Field1 = iter.ReadString()
case "field2":
obj.Field2 = iter.ReadUint64()
default:
iter.ReportError("bind object", "unexpected field")
}
}
}
}
func Benchmark_jsoniter_cb(b *testing.B) {
for n := 0; n < b.N; n++ {
iter := ParseString(`{"field1": "1", "field2": 2}`)
obj := TestObj{}
iter.ReadObjectCB(func(field string) {
switch field {
case "field1":
obj.Field1 = iter.ReadString()
case "field2":
obj.Field2 = iter.ReadUint64()
default:
iter.ReportError("bind object", "unexpected field")
}
})
}
}