-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterator_object.go
167 lines (161 loc) · 3.06 KB
/
iterator_object.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package jzon
// use after the first `"` is consumed
// will read the object field as well as the colon
func (it *Iterator) readObjectFieldAsSlice() (field []byte, err error) {
field, err = it.readStringAsSlice()
if err != nil {
return
}
c, err := it.nextToken()
if err != nil {
return
}
if c != ':' {
err = UnexpectedByteError{got: c, exp: ':'}
return
}
it.head++
return
}
// called only when a '"' is consumed
func (it *Iterator) readObjectField() (_ string, err error) {
field, err := it.readObjectFieldAsSlice()
if err != nil {
return "", err
}
return string(field), nil
}
func (it *Iterator) skipObjectField() error {
err := skipString(it, '"')
if err != nil {
return err
}
c, err := it.nextToken()
if err != nil {
return err
}
if c != ':' {
return UnexpectedByteError{got: c, exp: ':'}
}
it.head++
return nil
}
// ReadObjectBegin starts to read an object
func (it *Iterator) ReadObjectBegin() (_ bool, _ string, err error) {
c, err := it.nextToken()
if err != nil {
return
}
if c != '{' {
err = UnexpectedByteError{got: c, exp: '{'}
return
}
it.head++
c, err = it.nextToken()
if err != nil {
return
}
switch c {
case '}':
// no more items
it.head++
return
case '"':
it.head++
var fieldBytes []byte
fieldBytes, err = it.readObjectFieldAsSlice()
if err != nil {
return
}
return true, string(fieldBytes), nil
default:
err = UnexpectedByteError{got: c, exp: '}', exp2: '"'}
return
}
}
// ReadObjectMore tells if there is more field to read in the object
func (it *Iterator) ReadObjectMore() (_ bool, _ string, err error) {
c, err := it.nextToken()
if err != nil {
return
}
switch c {
case '}':
it.head++
return
case ',':
it.head++
c, err = it.nextToken()
if err != nil {
return
}
if c != '"' {
err = UnexpectedByteError{got: c, exp: '"'}
return
}
it.head++
var fieldBytes []byte
fieldBytes, err = it.readObjectFieldAsSlice()
if err != nil {
return
}
return true, string(fieldBytes), nil
default:
err = UnexpectedByteError{got: c, exp: '}', exp2: ','}
return
}
}
// ReadObjectCB reads the object with a callback
// The caller should make sure that the callback is correct
func (it *Iterator) ReadObjectCB(cb func(it *Iterator, field string) error) error {
c, err := it.nextToken()
if err != nil {
return err
}
if c != '{' {
return UnexpectedByteError{got: c, exp: '{'}
}
it.head++
c, err = it.nextToken()
if err != nil {
return err
}
if c == '}' {
it.head++
return nil
}
if c != '"' {
return UnexpectedByteError{got: c, exp: '"', exp2: '}'}
}
it.head++
for {
field, err := it.readObjectField()
if err != nil {
return err
}
if err := cb(it, field); err != nil {
return err
}
c, err = it.nextToken()
if err != nil {
return err
}
switch c {
case '}':
it.head++
return nil
case ',':
it.head++
c, err = it.nextToken()
if err != nil {
return err
}
if c != '"' {
return UnexpectedByteError{got: c, exp: '"'}
}
it.head++
default:
return UnexpectedByteError{got: c, exp: '}', exp2: ','}
}
}
}