-
Notifications
You must be signed in to change notification settings - Fork 0
/
iter.go
74 lines (62 loc) · 1.34 KB
/
iter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package json
// IterFunc is a little helper on top of Enter and ForMore methonds.
// It iterates over object or array and calls f for each value.
// If it iterates over array k will be nil.
// If it iterates over object k is decoded using Key which doesn't decode escape sequences.
// It reads object or array to the end unless f returned an error.
func (d *Decoder) IterFunc(b []byte, st int, tp byte, f func(k, v []byte) error) (i int, err error) {
var k, v []byte
i, err = d.Enter(b, st, tp)
if err != nil {
return i, err
}
for d.ForMore(b, &i, tp, &err) {
if tp == Object {
k, i, err = d.Key(b, i)
if err != nil {
return i, err
}
}
v, i, err = d.Raw(b, i)
if err != nil {
return i, err
}
err = f(k, v)
if err != nil {
return i, err
}
}
if err != nil {
return i, err
}
return i, nil
}
// IterFunc is a little helper on top of Enter and ForMore methonds.
// See Decoder.IterFunc for more details.
func (r *Reader) IterFunc(tp byte, f func(k, v []byte) error) (err error) {
var k, v []byte
err = r.Enter(tp)
if err != nil {
return err
}
for r.ForMore(tp, &err) {
if tp == Object {
k, err = r.Key()
if err != nil {
return err
}
}
v, err = r.Raw()
if err != nil {
return err
}
err = f(k, v)
if err != nil {
return err
}
}
if err != nil {
return err
}
return nil
}