-
Notifications
You must be signed in to change notification settings - Fork 90
/
parsed_object.go
433 lines (404 loc) · 10.3 KB
/
parsed_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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* 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"
)
// Object represents a JSON object.
type Object struct {
// Complete tape
tape ParsedJson
// offset of the next entry to be decoded
off int
}
// Map will unmarshal into a map[string]interface{}
// See Iter.Interface() for a reference on value types.
func (o *Object) Map(dst map[string]interface{}) (map[string]interface{}, error) {
if dst == nil {
dst = make(map[string]interface{})
}
var tmp Iter
for {
name, t, err := o.NextElement(&tmp)
if err != nil {
return nil, err
}
if t == TypeNone {
// Done
break
}
dst[name], err = tmp.Interface()
if err != nil {
return nil, fmt.Errorf("parsing element %q: %w", name, err)
}
}
return dst, nil
}
// Parse will return all elements and iterators.
// An optional destination can be given.
// The Object will be consumed.
func (o *Object) Parse(dst *Elements) (*Elements, error) {
if dst == nil {
dst = &Elements{
Elements: make([]Element, 0, 5),
Index: make(map[string]int, 5),
}
} else {
dst.Elements = dst.Elements[:0]
for k := range dst.Index {
delete(dst.Index, k)
}
}
var tmp Iter
for {
name, t, err := o.NextElement(&tmp)
if err != nil {
return dst, err
}
if t == TypeNone {
// Done
break
}
dst.Index[name] = len(dst.Elements)
dst.Elements = append(dst.Elements, Element{
Name: name,
Type: t,
Iter: tmp,
})
}
return dst, nil
}
// FindKey will return a single named element.
// An optional destination can be given.
// The method will return nil if the element cannot be found.
// This should only be used to locate a single key where the object is no longer needed.
// The object will not be advanced.
func (o *Object) FindKey(key string, dst *Element) *Element {
tmp := o.tape.Iter()
tmp.off = o.off
for {
typ := tmp.Advance()
// We want name and at least one value.
if typ != TypeString || tmp.off+1 >= len(tmp.tape.Tape) {
return nil
}
// Advance must be string or end of object
offset := tmp.cur
length := tmp.tape.Tape[tmp.off]
if int(length) != len(key) {
// Skip the value.
t := tmp.Advance()
if t == TypeNone {
return nil
}
continue
}
// Read name
name, err := tmp.tape.stringByteAt(offset, length)
if err != nil {
return nil
}
if string(name) != key {
// Skip the value
tmp.Advance()
continue
}
if dst == nil {
dst = &Element{}
}
dst.Name = key
dst.Type, err = tmp.AdvanceIter(&dst.Iter)
if err != nil {
return nil
}
return dst
}
}
// ForEach will call back fn for each key.
// A key filter can be provided for optional filtering.
func (o *Object) ForEach(fn func(key []byte, i Iter), onlyKeys map[string]struct{}) error {
tmp := o.tape.Iter()
tmp.off = o.off
n := 0
for {
typ := tmp.Advance()
// We want name and at least one value.
if typ != TypeString || tmp.off+1 >= len(tmp.tape.Tape) {
if typ == TypeNone {
return nil
}
return fmt.Errorf("object: unexpected name tag %v", tmp.t)
}
// Advance must be string or end of object
offset := tmp.cur
length := tmp.tape.Tape[tmp.off]
// Read name
name, err := tmp.tape.stringByteAt(offset, length)
if err != nil {
return fmt.Errorf("getting object name: %w", err)
}
if len(onlyKeys) > 0 {
if _, ok := onlyKeys[string(name)]; !ok {
// Skip the value
t := tmp.Advance()
if t == TypeNone {
return nil
}
}
}
t := tmp.Advance()
if t == TypeNone {
return nil
}
fn(name, tmp)
n++
if n == len(onlyKeys) {
return nil
}
}
}
// DeleteElems will call back fn for each key.
// If true is returned, the key+value is deleted.
// A key filter can be provided for optional filtering.
// If fn is nil all elements in onlyKeys will be deleted.
// If both are nil all elements are deleted.
func (o *Object) DeleteElems(fn func(key []byte, i Iter) bool, onlyKeys map[string]struct{}) error {
tmp := o.tape.Iter()
tmp.off = o.off
n := 0
for {
typ := tmp.Advance()
// We want name and at least one value.
if typ != TypeString || tmp.off+1 >= len(tmp.tape.Tape) {
if typ == TypeNone {
return nil
}
return fmt.Errorf("object: unexpected name tag %v", tmp.t)
}
startO := tmp.off - 1
// Advance must be string or end of object
offset := tmp.cur
length := tmp.tape.Tape[tmp.off]
// Read name
name, err := tmp.tape.stringByteAt(offset, length)
if err != nil {
return fmt.Errorf("getting object name: %w", err)
}
if len(onlyKeys) > 0 {
if _, ok := onlyKeys[string(name)]; !ok {
// Skip the value
t := tmp.Advance()
if t == TypeNone {
return nil
}
continue
}
}
t := tmp.Advance()
if t == TypeNone {
return nil
}
if fn == nil || fn(name, tmp) {
end := tmp.off + tmp.addNext
skip := uint64(end - startO)
for i := startO; i < end; i++ {
tmp.tape.Tape[i] = (uint64(TagNop) << JSONTAGOFFSET) | skip
skip--
}
}
n++
if n == len(onlyKeys) {
return nil
}
}
}
// ErrPathNotFound is returned
var ErrPathNotFound = errors.New("path not found")
// FindPath allows searching for fields and objects by path.
// Separate each object name by /.
// For example `Image/Url` will search the current object for an "Image"
// object and return the value of the "Url" element.
// ErrPathNotFound is returned if any part of the path cannot be found.
// If the tape contains an error it will be returned.
// The object will not be advanced.
func (o *Object) FindPath(dst *Element, path ...string) (*Element, error) {
if len(path) == 0 {
return dst, ErrPathNotFound
}
tmp := o.tape.Iter()
tmp.off = o.off
key := path[0]
path = path[1:]
for {
typ := tmp.Advance()
// We want name and at least one value.
if typ != TypeString || tmp.off+1 >= len(tmp.tape.Tape) {
return dst, ErrPathNotFound
}
// Advance must be string or end of object
offset := tmp.cur
length := tmp.tape.Tape[tmp.off]
if int(length) != len(key) {
// Skip the value.
t := tmp.Advance()
if t == TypeNone {
// Not found...
return dst, ErrPathNotFound
}
continue
}
// Read name
name, err := tmp.tape.stringByteAt(offset, length)
if err != nil {
return dst, err
}
if string(name) != key {
// Skip the value
tmp.Advance()
continue
}
// Done...
if len(path) == 0 {
if dst == nil {
dst = &Element{}
}
dst.Name = key
dst.Type, err = tmp.AdvanceIter(&dst.Iter)
if err != nil {
return dst, err
}
return dst, nil
}
t, err := tmp.AdvanceIter(&tmp)
if err != nil {
return dst, err
}
if t != TypeObject {
return dst, fmt.Errorf("value of key %v is not an object", key)
}
key = path[0]
path = path[1:]
}
}
// NextElement sets dst to the next element and returns the name.
// TypeNone with nil error will be returned if there are no more elements.
func (o *Object) NextElement(dst *Iter) (name string, t Type, err error) {
n, t, err := o.NextElementBytes(dst)
return string(n), t, err
}
// NextElementBytes sets dst to the next element and returns the name.
// TypeNone with nil error will be returned if there are no more elements.
// Contrary to NextElement this will not cause allocations.
func (o *Object) NextElementBytes(dst *Iter) (name []byte, t Type, err error) {
if o.off >= len(o.tape.Tape) {
return nil, TypeNone, nil
}
// Advance must be string or end of object
v := o.tape.Tape[o.off]
switch Tag(v >> 56) {
case TagString:
// Read name:
// We want name and at least one value.
if o.off+2 >= len(o.tape.Tape) {
return nil, TypeNone, fmt.Errorf("parsing object element name: unexpected end of tape")
}
length := o.tape.Tape[o.off+1]
offset := v & JSONVALUEMASK
name, err = o.tape.stringByteAt(offset, length)
if err != nil {
return nil, TypeNone, fmt.Errorf("parsing object element name: %w", err)
}
o.off += 2
case TagObjectEnd:
return nil, TypeNone, nil
case TagNop:
o.off += int(v & JSONVALUEMASK)
return o.NextElementBytes(dst)
default:
return nil, TypeNone, fmt.Errorf("object: unexpected tag %c", byte(v>>56))
}
// Read element type
v = o.tape.Tape[o.off]
// Move to value (if any)
o.off++
// Set dst
dst.cur = v & JSONVALUEMASK
dst.t = Tag(v >> 56)
dst.off = o.off
dst.tape = o.tape
dst.calcNext(false)
elemSize := dst.addNext
dst.calcNext(true)
if dst.off+elemSize > len(dst.tape.Tape) {
return nil, TypeNone, errors.New("element extends beyond tape")
}
dst.tape.Tape = dst.tape.Tape[:dst.off+elemSize]
// Skip to next element
o.off += elemSize
return name, TagToType[dst.t], nil
}
// Element represents an element in an object.
type Element struct {
// Name of the element
Name string
// Type of the element
Type Type
// Iter containing the element
Iter Iter
}
// Elements contains all elements in an object
// kept in original order.
// And index contains lookup for object keys.
type Elements struct {
Elements []Element
Index map[string]int
}
// Lookup a key in elements and return the element.
// Returns nil if key doesn't exist.
// Keys are case sensitive.
func (e Elements) Lookup(key string) *Element {
idx, ok := e.Index[key]
if !ok {
return nil
}
return &e.Elements[idx]
}
// MarshalJSON will marshal the entire remaining scope of the iterator.
func (e Elements) MarshalJSON() ([]byte, error) {
return e.MarshalJSONBuffer(nil)
}
// MarshalJSONBuffer will marshal all elements.
// An optional buffer can be provided for fewer allocations.
// Output will be appended to the destination.
func (e Elements) MarshalJSONBuffer(dst []byte) ([]byte, error) {
dst = append(dst, '{')
for i, elem := range e.Elements {
dst = append(dst, '"')
dst = escapeBytes(dst, []byte(elem.Name))
dst = append(dst, '"', ':')
var err error
dst, err = elem.Iter.MarshalJSONBuffer(dst)
if err != nil {
return nil, err
}
if i < len(e.Elements)-1 {
dst = append(dst, ',')
}
}
dst = append(dst, '}')
return dst, nil
}