-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathparsed_array.go
345 lines (326 loc) · 8.4 KB
/
parsed_array.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* MinIO Cloud Storage, (C) 2020 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package simdjson
import (
"errors"
"fmt"
"math"
)
// Array represents a JSON array.
// There are methods that allows to get full arrays if the value type is the same.
// Otherwise an iterator can be retrieved.
type Array struct {
tape ParsedJson
off int
}
// Iter returns the array as an iterator.
// This can be used for parsing mixed content arrays.
// The first value is ready with a call to Advance.
// Calling after last element should have TypeNone.
func (a *Array) Iter() Iter {
i := Iter{
tape: a.tape,
off: a.off,
}
return i
}
// ForEach calls the provided function for every element.
func (a *Array) ForEach(fn func(i Iter)) {
i := a.Iter()
for {
t := i.Advance()
if t == TypeNone {
break
}
fn(i)
}
return
}
// DeleteElems calls the provided function for every element.
// If the function returns true the element is deleted in the array.
func (a *Array) DeleteElems(fn func(i Iter) bool) {
i := a.Iter()
for {
t := i.Advance()
if t == TypeNone {
break
}
if fn(i) {
startO := i.off - 1
end := i.off + i.addNext
skip := uint64(end - startO)
for off := startO; off < end; off++ {
i.tape.Tape[off] = (uint64(TagNop) << JSONTAGOFFSET) | skip
skip--
}
}
}
return
}
// FirstType will return the type of the first element.
// If there are no elements, TypeNone is returned.
func (a *Array) FirstType() Type {
iter := a.Iter()
return iter.PeekNext()
}
// MarshalJSON will marshal the entire remaining scope of the iterator.
func (a *Array) MarshalJSON() ([]byte, error) {
return a.MarshalJSONBuffer(nil)
}
// MarshalJSONBuffer will marshal all elements.
// An optional buffer can be provided for fewer allocations.
// Output will be appended to the destination.
func (a *Array) MarshalJSONBuffer(dst []byte) ([]byte, error) {
dst = append(dst, '[')
i := a.Iter()
var elem Iter
for {
t, err := i.AdvanceIter(&elem)
if err != nil {
return nil, err
}
if t == TypeNone {
break
}
dst, err = elem.MarshalJSONBuffer(dst)
if err != nil {
return nil, err
}
if i.PeekNextTag() == TagArrayEnd {
break
}
dst = append(dst, ',')
}
if i.PeekNextTag() != TagArrayEnd {
return nil, errors.New("expected TagArrayEnd as final tag in array")
}
dst = append(dst, ']')
return dst, nil
}
// Interface returns the array as a slice of interfaces.
// See Iter.Interface() for a reference on value types.
func (a *Array) Interface() ([]interface{}, error) {
// Estimate length. Assume one value per element.
lenEst := (len(a.tape.Tape) - a.off - 1) / 2
if lenEst < 0 {
lenEst = 0
}
dst := make([]interface{}, 0, lenEst)
i := a.Iter()
for i.Advance() != TypeNone {
elem, err := i.Interface()
if err != nil {
return nil, err
}
dst = append(dst, elem)
}
return dst, nil
}
// AsFloat returns the array values as float.
// Integers are automatically converted to float.
func (a *Array) AsFloat() ([]float64, error) {
// Estimate length
lenEst := (len(a.tape.Tape) - a.off - 1) / 2
if lenEst < 0 {
lenEst = 0
}
dst := make([]float64, 0, lenEst)
readArray:
for {
tag := Tag(a.tape.Tape[a.off] >> 56)
a.off++
switch tag {
case TagFloat:
if len(a.tape.Tape) <= a.off {
return nil, errors.New("corrupt input: expected float, but no more values")
}
dst = append(dst, math.Float64frombits(a.tape.Tape[a.off]))
case TagInteger:
if len(a.tape.Tape) <= a.off {
return nil, errors.New("corrupt input: expected integer, but no more values")
}
dst = append(dst, float64(int64(a.tape.Tape[a.off])))
case TagUint:
if len(a.tape.Tape) <= a.off {
return nil, errors.New("corrupt input: expected integer, but no more values")
}
dst = append(dst, float64(a.tape.Tape[a.off]))
case TagArrayEnd:
break readArray
default:
return nil, fmt.Errorf("unable to convert type %v to float", tag)
}
a.off++
}
return dst, nil
}
// AsInteger returns the array values as int64 values.
// Uints/Floats are automatically converted to int64 if they fit within the range.
func (a *Array) AsInteger() ([]int64, error) {
// Estimate length
lenEst := (len(a.tape.Tape) - a.off - 1) / 2
if lenEst < 0 {
lenEst = 0
}
dst := make([]int64, 0, lenEst)
readArray:
for {
tag := Tag(a.tape.Tape[a.off] >> 56)
a.off++
switch tag {
case TagFloat:
if len(a.tape.Tape) <= a.off {
return nil, errors.New("corrupt input: expected float, but no more values")
}
val := math.Float64frombits(a.tape.Tape[a.off])
if val > math.MaxInt64 {
return nil, errors.New("float value overflows int64")
}
if val < math.MinInt64 {
return nil, errors.New("float value underflows int64")
}
dst = append(dst, int64(val))
case TagInteger:
if len(a.tape.Tape) <= a.off {
return nil, errors.New("corrupt input: expected integer, but no more values")
}
dst = append(dst, int64(a.tape.Tape[a.off]))
case TagUint:
if len(a.tape.Tape) <= a.off {
return nil, errors.New("corrupt input: expected integer, but no more values")
}
val := a.tape.Tape[a.off]
if val > math.MaxInt64 {
return nil, errors.New("unsigned integer value overflows int64")
}
dst = append(dst)
case TagArrayEnd:
break readArray
default:
return nil, fmt.Errorf("unable to convert type %v to integer", tag)
}
a.off++
}
return dst, nil
}
// AsUint64 returns the array values as float.
// Uints/Floats are automatically converted to uint64 if they fit within the range.
func (a *Array) AsUint64() ([]uint64, error) {
// Estimate length
lenEst := (len(a.tape.Tape) - a.off - 1) / 2
if lenEst < 0 {
lenEst = 0
}
dst := make([]uint64, 0, lenEst)
readArray:
for {
tag := Tag(a.tape.Tape[a.off] >> 56)
a.off++
switch tag {
case TagFloat:
if len(a.tape.Tape) <= a.off {
return nil, errors.New("corrupt input: expected float, but no more values")
}
val := math.Float64frombits(a.tape.Tape[a.off])
if val > math.MaxInt64 {
return nil, errors.New("float value overflows uint64")
}
if val < 0 {
return nil, errors.New("float value is negative")
}
dst = append(dst, uint64(val))
case TagInteger:
if len(a.tape.Tape) <= a.off {
return nil, errors.New("corrupt input: expected integer, but no more values")
}
val := int64(a.tape.Tape[a.off])
if val < 0 {
return nil, errors.New("int64 value is negative")
}
dst = append(dst, uint64(val))
case TagUint:
if len(a.tape.Tape) <= a.off {
return nil, errors.New("corrupt input: expected integer, but no more values")
}
dst = append(dst, a.tape.Tape[a.off])
case TagArrayEnd:
break readArray
default:
return nil, fmt.Errorf("unable to convert type %v to integer", tag)
}
a.off++
}
return dst, nil
}
// AsString returns the array values as a slice of strings.
// No conversion is done.
func (a *Array) AsString() ([]string, error) {
// Estimate length
lenEst := len(a.tape.Tape) - a.off - 1
if lenEst < 0 {
lenEst = 0
}
dst := make([]string, 0, lenEst)
i := a.Iter()
var elem Iter
for {
t, err := i.AdvanceIter(&elem)
if err != nil {
return nil, err
}
switch t {
case TypeNone:
return dst, nil
case TypeString:
s, err := elem.String()
if err != nil {
return nil, err
}
dst = append(dst, s)
default:
return nil, fmt.Errorf("element in array is not string, but %v", t)
}
}
}
// AsStringCvt returns the array values as a slice of strings.
// Scalar types are converted.
// Root, Object and Arrays are not supported an will return an error if found.
func (a *Array) AsStringCvt() ([]string, error) {
// Estimate length
lenEst := len(a.tape.Tape) - a.off - 1
if lenEst < 0 {
lenEst = 0
}
dst := make([]string, 0, lenEst)
i := a.Iter()
var elem Iter
for {
t, err := i.AdvanceIter(&elem)
if err != nil {
return nil, err
}
switch t {
case TypeNone:
return dst, nil
default:
s, err := elem.StringCvt()
if err != nil {
return nil, err
}
dst = append(dst, s)
}
}
}