forked from minio/simdjson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsed_array.go
229 lines (214 loc) · 5.68 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
/*
* 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
}
// 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 float.
// Integers are automatically converted to float.
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
}
// 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 {
}
dst = append(dst, s)
default:
return nil, fmt.Errorf("element in array is not string, but %v", t)
}
}
}