forked from minio/simdjson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsed_object.go
239 lines (222 loc) · 5.77 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
/*
* 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 {
var tmp Iter
obj := *o
for {
name, t, err := obj.NextElementBytes(&tmp)
if err != nil {
return nil
}
if t == TypeNone {
// Done
break
}
if string(name) == key {
if dst == nil {
return &Element{
Name: key,
Type: t,
Iter: tmp,
}
}
dst.Name = key
dst.Iter = tmp
dst.Type = t
return dst
}
}
return nil
}
// 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
default:
return nil, TypeNone, fmt.Errorf("object: unexpected tag %v", string(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
}