Frames

Untitled

0
1package gojay
2
3import (
4 "fmt"
5 "io"
6)
7
8// UnmarshalJSONArray parses the JSON-encoded data and stores the result in the value pointed to by v.
9//
10// v must implement UnmarshalerJSONArray.
11//
12// If a JSON value is not appropriate for a given target type, or if a JSON number
13// overflows the target type, UnmarshalJSONArray skips that field and completes the unmarshaling as best it can.
14func UnmarshalJSONArray(data []byte, v UnmarshalerJSONArray) error {
15 dec := borrowDecoder(nil, 0)
16 defer dec.Release()
17 dec.data = make([]byte, len(data))
18 copy(dec.data, data)
19 dec.length = len(data)
20 _, err := dec.decodeArray(v)
21 if err != nil {
22 return err
23 }
24 if dec.err != nil {
25 return dec.err
26 }
27 return nil
28}
29
30// UnmarshalJSONObject parses the JSON-encoded data and stores the result in the value pointed to by v.
31//
32// v must implement UnmarshalerJSONObject.
33//
34// If a JSON value is not appropriate for a given target type, or if a JSON number
35// overflows the target type, UnmarshalJSONObject skips that field and completes the unmarshaling as best it can.
36func UnmarshalJSONObject(data []byte, v UnmarshalerJSONObject) error {
37 dec := borrowDecoder(nil, 0)
38 defer dec.Release()
39 dec.data = make([]byte, len(data))
40 copy(dec.data, data)
41 dec.length = len(data)
42 _, err := dec.decodeObject(v)
43 if err != nil {
44 return err
45 }
46 if dec.err != nil {
47 return dec.err
48 }
49 return nil
50}
51
52// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
53// If v is nil, not an implementation of UnmarshalerJSONObject or UnmarshalerJSONArray or not one of the following types:
54// *string, **string, *int, **int, *int8, **int8, *int16, **int16, *int32, **int32, *int64, **int64, *uint8, **uint8, *uint16, **uint16,
55// *uint32, **uint32, *uint64, **uint64, *float64, **float64, *float32, **float32, *bool, **bool
56// Unmarshal returns an InvalidUnmarshalError.
57//
58//
59// If a JSON value is not appropriate for a given target type, or if a JSON number
60// overflows the target type, Unmarshal skips that field and completes the unmarshaling as best it can.
61// If no more serious errors are encountered, Unmarshal returns an UnmarshalTypeError describing the earliest such error.
62// In any case, it's not guaranteed that all the remaining fields following the problematic one will be unmarshaled into the target object.
63func Unmarshal(data []byte, v interface{}) error {
64 var err error
65 var dec *Decoder
66 switch vt := v.(type) {
67 case *string:
68 dec = borrowDecoder(nil, 0)
69 dec.length = len(data)
70 dec.data = data
71 err = dec.decodeString(vt)
72 case **string:
73 dec = borrowDecoder(nil, 0)
74 dec.length = len(data)
75 dec.data = data
76 err = dec.decodeStringNull(vt)
77 case *int:
78 dec = borrowDecoder(nil, 0)
79 dec.length = len(data)
80 dec.data = data
81 err = dec.decodeInt(vt)
82 case **int:
83 dec = borrowDecoder(nil, 0)
84 dec.length = len(data)
85 dec.data = data
86 err = dec.decodeIntNull(vt)
87 case *int8:
88 dec = borrowDecoder(nil, 0)
89 dec.length = len(data)
90 dec.data = data
91 err = dec.decodeInt8(vt)
92 case **int8:
93 dec = borrowDecoder(nil, 0)
94 dec.length = len(data)
95 dec.data = data
96 err = dec.decodeInt8Null(vt)
97 case *int16:
98 dec = borrowDecoder(nil, 0)
99 dec.length = len(data)
100 dec.data = data
101 err = dec.decodeInt16(vt)
102 case **int16:
103 dec = borrowDecoder(nil, 0)
104 dec.length = len(data)
105 dec.data = data
106 err = dec.decodeInt16Null(vt)
107 case *int32:
108 dec = borrowDecoder(nil, 0)
109 dec.length = len(data)
110 dec.data = data
111 err = dec.decodeInt32(vt)
112 case **int32:
113 dec = borrowDecoder(nil, 0)
114 dec.length = len(data)
115 dec.data = data
116 err = dec.decodeInt32Null(vt)
117 case *int64:
118 dec = borrowDecoder(nil, 0)
119 dec.length = len(data)
120 dec.data = data
121 err = dec.decodeInt64(vt)
122 case **int64:
123 dec = borrowDecoder(nil, 0)
124 dec.length = len(data)
125 dec.data = data
126 err = dec.decodeInt64Null(vt)
127 case *uint8:
128 dec = borrowDecoder(nil, 0)
129 dec.length = len(data)
130 dec.data = data
131 err = dec.decodeUint8(vt)
132 case **uint8:
133 dec = borrowDecoder(nil, 0)
134 dec.length = len(data)
135 dec.data = data
136 err = dec.decodeUint8Null(vt)
137 case *uint16:
138 dec = borrowDecoder(nil, 0)
139 dec.length = len(data)
140 dec.data = data
141 err = dec.decodeUint16(vt)
142 case **uint16:
143 dec = borrowDecoder(nil, 0)
144 dec.length = len(data)
145 dec.data = data
146 err = dec.decodeUint16Null(vt)
147 case *uint32:
148 dec = borrowDecoder(nil, 0)
149 dec.length = len(data)
150 dec.data = data
151 err = dec.decodeUint32(vt)
152 case **uint32:
153 dec = borrowDecoder(nil, 0)
154 dec.length = len(data)
155 dec.data = data
156 err = dec.decodeUint32Null(vt)
157 case *uint64:
158 dec = borrowDecoder(nil, 0)
159 dec.length = len(data)
160 dec.data = data
161 err = dec.decodeUint64(vt)
162 case **uint64:
163 dec = borrowDecoder(nil, 0)
164 dec.length = len(data)
165 dec.data = data
166 err = dec.decodeUint64Null(vt)
167 case *float64:
168 dec = borrowDecoder(nil, 0)
169 dec.length = len(data)
170 dec.data = data
171 err = dec.decodeFloat64(vt)
172 case **float64:
173 dec = borrowDecoder(nil, 0)
174 dec.length = len(data)
175 dec.data = data
176 err = dec.decodeFloat64Null(vt)
177 case *float32:
178 dec = borrowDecoder(nil, 0)
179 dec.length = len(data)
180 dec.data = data
181 err = dec.decodeFloat32(vt)
182 case **float32:
183 dec = borrowDecoder(nil, 0)
184 dec.length = len(data)
185 dec.data = data
186 err = dec.decodeFloat32Null(vt)
187 case *bool:
188 dec = borrowDecoder(nil, 0)
189 dec.length = len(data)
190 dec.data = data
191 err = dec.decodeBool(vt)
192 case **bool:
193 dec = borrowDecoder(nil, 0)
194 dec.length = len(data)
195 dec.data = data
196 err = dec.decodeBoolNull(vt)
197 case UnmarshalerJSONObject:
198 dec = borrowDecoder(nil, 0)
199 dec.length = len(data)
200 dec.data = make([]byte, len(data))
201 copy(dec.data, data)
202 _, err = dec.decodeObject(vt)
203 case UnmarshalerJSONArray:
204 dec = borrowDecoder(nil, 0)
205 dec.length = len(data)
206 dec.data = make([]byte, len(data))
207 copy(dec.data, data)
208 _, err = dec.decodeArray(vt)
209 case *interface{}:
210 dec = borrowDecoder(nil, 0)
211 dec.length = len(data)
212 dec.data = make([]byte, len(data))
213 copy(dec.data, data)
214 err = dec.decodeInterface(vt)
215 default:
216 return InvalidUnmarshalError(fmt.Sprintf(invalidUnmarshalErrorMsg, vt))
217 }
218 defer dec.Release()
219 if err != nil {
220 return err
221 }
222 return dec.err
223}
224
225// UnmarshalerJSONObject is the interface to implement to decode a JSON Object.
226type UnmarshalerJSONObject interface {
227 UnmarshalJSONObject(*Decoder, string) error
228 NKeys() int
229}
230
231// UnmarshalerJSONArray is the interface to implement to decode a JSON Array.
232type UnmarshalerJSONArray interface {
233 UnmarshalJSONArray(*Decoder) error
234}
235
236// A Decoder reads and decodes JSON values from an input stream.
237type Decoder struct {
238 r io.Reader
239 data []byte
240 err error
241 isPooled byte
242 called byte
243 child byte
244 cursor int
245 length int
246 keysDone int
247 arrayIndex int
248}
249
250// Decode reads the next JSON-encoded value from the decoder's input (io.Reader) and stores it in the value pointed to by v.
251//
252// See the documentation for Unmarshal for details about the conversion of JSON into a Go value.
253// The differences between Decode and Unmarshal are:
254// - Decode reads from an io.Reader in the Decoder, whereas Unmarshal reads from a []byte
255// - Decode leaves to the user the option of borrowing and releasing a Decoder, whereas Unmarshal internally always borrows a Decoder and releases it when the unmarshaling is completed
256func (dec *Decoder) Decode(v interface{}) error {
257 if dec.isPooled == 1 {
258 panic(InvalidUsagePooledDecoderError("Invalid usage of pooled decoder"))
259 }
260 var err error
261 switch vt := v.(type) {
262 case *string:
263 err = dec.decodeString(vt)
264 case **string:
265 err = dec.decodeStringNull(vt)
266 case *int:
267 err = dec.decodeInt(vt)
268 case **int:
269 err = dec.decodeIntNull(vt)
270 case *int8:
271 err = dec.decodeInt8(vt)
272 case **int8:
273 err = dec.decodeInt8Null(vt)
274 case *int16:
275 err = dec.decodeInt16(vt)
276 case **int16:
277 err = dec.decodeInt16Null(vt)
278 case *int32:
279 err = dec.decodeInt32(vt)
280 case **int32:
281 err = dec.decodeInt32Null(vt)
282 case *int64:
283 err = dec.decodeInt64(vt)
284 case **int64:
285 err = dec.decodeInt64Null(vt)
286 case *uint8:
287 err = dec.decodeUint8(vt)
288 case **uint8:
289 err = dec.decodeUint8Null(vt)
290 case *uint16:
291 err = dec.decodeUint16(vt)
292 case **uint16:
293 err = dec.decodeUint16Null(vt)
294 case *uint32:
295 err = dec.decodeUint32(vt)
296 case **uint32:
297 err = dec.decodeUint32Null(vt)
298 case *uint64:
299 err = dec.decodeUint64(vt)
300 case **uint64:
301 err = dec.decodeUint64Null(vt)
302 case *float64:
303 err = dec.decodeFloat64(vt)
304 case **float64:
305 err = dec.decodeFloat64Null(vt)
306 case *float32:
307 err = dec.decodeFloat32(vt)
308 case **float32:
309 err = dec.decodeFloat32Null(vt)
310 case *bool:
311 err = dec.decodeBool(vt)
312 case **bool:
313 err = dec.decodeBoolNull(vt)
314 case UnmarshalerJSONObject:
315 _, err = dec.decodeObject(vt)
316 case UnmarshalerJSONArray:
317 _, err = dec.decodeArray(vt)
318 case *EmbeddedJSON:
319 err = dec.decodeEmbeddedJSON(vt)
320 case *interface{}:
321 err = dec.decodeInterface(vt)
322 default:
323 return InvalidUnmarshalError(fmt.Sprintf(invalidUnmarshalErrorMsg, vt))
324 }
325 if err != nil {
326 return err
327 }
328 return dec.err
329}
330
331// Non exported
332
333func isDigit(b byte) bool {
334 switch b {
335 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
336 return true
337 default:
338 return false
339 }
340}
341
342func (dec *Decoder) read() bool {
343 if dec.r != nil {
344 // if we reach the end, double the buffer to ensure there's always more space
345 if len(dec.data) == dec.length {
346 nLen := dec.length * 2
347 if nLen == 0 {
348 nLen = 512
349 }
350 Buf := make([]byte, nLen, nLen)
351 copy(Buf, dec.data)
352 dec.data = Buf
353 }
354 var n int
355 var err error
356 for n == 0 {
357 n, err = dec.r.Read(dec.data[dec.length:])
358 if err != nil {
359 if err != io.EOF {
360 dec.err = err
361 return false
362 }
363 if n == 0 {
364 return false
365 }
366 dec.length = dec.length + n
367 return true
368 }
369 }
370 dec.length = dec.length + n
371 return true
372 }
373 return false
374}
375
376func (dec *Decoder) nextChar() byte {
377 for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
378 switch dec.data[dec.cursor] {
379 case ' ', '\n', '\t', '\r', ',':
380 continue
381 }
382 d := dec.data[dec.cursor]
383 return d
384 }
385 return 0
386}
387