-
Notifications
You must be signed in to change notification settings - Fork 22
/
decoder.go
518 lines (490 loc) · 11.9 KB
/
decoder.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
// Package json decodes JSON.
package json
import (
"encoding/json"
"fmt"
"io"
"reflect"
"strconv"
"unsafe"
)
// A Decoder decodes JSON values from an input stream.
type Decoder struct {
scanner Scanner
state func(*Decoder) ([]byte, error)
stack
}
// NewDecoder returns a new Decoder for the supplied Reader r.
func NewDecoder(r io.Reader) *Decoder {
return NewDecoderBuffer(r, make([]byte, 8192))
}
// NewDecoderBuffer returns a new Decoder for the supplier Reader r, using
// the []byte buf provided for working storage.
func NewDecoderBuffer(r io.Reader, buf []byte) *Decoder {
return &Decoder{
scanner: Scanner{
br: byteReader{
data: buf[:0],
r: r,
},
},
state: (*Decoder).stateValue,
}
}
type stack []bool
func (s *stack) push(v bool) {
*s = append(*s, v)
}
func (s *stack) pop() bool {
*s = (*s)[:len(*s)-1]
if len(*s) == 0 {
return false
}
return (*s)[len(*s)-1]
}
func (s *stack) len() int { return len(*s) }
// Token returns the next JSON token in the input stream.
// At the end of the input stream, Token returns nil, io.EOF.
//
// Token guarantees that the delimiters [ ] { } it returns are
// properly nested and matched: if Token encounters an unexpected
// delimiter in the input, it will return an error.
//
// The input stream consists of basic JSON values—bool, string,
// number, and null—along with delimiters [ ] { } of type json.Delim
// to mark the start and end of arrays and objects.
// Commas and colons are elided.
//
// Note: this API is provided for compatibility with the encoding/json
// package and carries a significant allocation cost. See NextToken for
// a more efficient API.
func (d *Decoder) Token() (json.Token, error) {
tok, err := d.NextToken()
if err != nil {
return nil, err
}
switch tok[0] {
case '{', '[', ']', '}':
return json.Delim(tok[0]), nil
case 't', 'f':
return tok[0] == 't', nil
case 'n':
return nil, nil
case '"':
return string(tok[1 : len(tok)-1]), nil
default:
return strconv.ParseFloat(bytesToString(tok), 64)
}
}
// NextToken returns a []byte referencing the next logical token in the stream.
// The []byte is valid until Token is called again.
// At the end of the input stream, Token returns nil, io.EOF.
//
// Token guarantees that the delimiters [ ] { } it returns are properly nested
// and matched: if Token encounters an unexpected delimiter in the input, it
// will return an error.
//
// A valid token begins with one of the following:
//
// { Object start
// [ Array start
// } Object end
// ] Array End
// t JSON true
// f JSON false
// n JSON null
// " A string, possibly containing backslash escaped entites.
// -, 0-9 A number
//
// Commas and colons are elided.
func (d *Decoder) NextToken() ([]byte, error) {
return d.state(d)
}
func (d *Decoder) stateObjectString() ([]byte, error) {
tok := d.scanner.Next()
if len(tok) < 1 {
return nil, io.ErrUnexpectedEOF
}
switch tok[0] {
case '}':
inObj := d.pop()
switch {
case d.len() == 0:
d.state = (*Decoder).stateEnd
case inObj:
d.state = (*Decoder).stateObjectComma
case !inObj:
d.state = (*Decoder).stateArrayComma
}
return tok, nil
case '"':
d.state = (*Decoder).stateObjectColon
return tok, nil
default:
return nil, fmt.Errorf("stateObjectString: missing string key")
}
}
func (d *Decoder) stateObjectColon() ([]byte, error) {
tok := d.scanner.Next()
if len(tok) < 1 {
return nil, io.ErrUnexpectedEOF
}
switch tok[0] {
case Colon:
d.state = (*Decoder).stateObjectValue
return d.NextToken()
default:
return tok, fmt.Errorf("stateObjectColon: expecting colon")
}
}
func (d *Decoder) stateObjectValue() ([]byte, error) {
tok := d.scanner.Next()
if len(tok) < 1 {
return nil, io.ErrUnexpectedEOF
}
switch tok[0] {
case '{':
d.state = (*Decoder).stateObjectString
d.push(true)
return tok, nil
case '[':
d.state = (*Decoder).stateArrayValue
d.push(false)
return tok, nil
default:
d.state = (*Decoder).stateObjectComma
return tok, nil
}
}
func (d *Decoder) stateObjectComma() ([]byte, error) {
tok := d.scanner.Next()
if len(tok) < 1 {
return nil, io.ErrUnexpectedEOF
}
switch tok[0] {
case '}':
inObj := d.pop()
switch {
case d.len() == 0:
d.state = (*Decoder).stateEnd
case inObj:
d.state = (*Decoder).stateObjectComma
case !inObj:
d.state = (*Decoder).stateArrayComma
}
return tok, nil
case Comma:
d.state = (*Decoder).stateObjectString
return d.NextToken()
default:
return tok, fmt.Errorf("stateObjectComma: expecting comma")
}
}
func (d *Decoder) stateArrayValue() ([]byte, error) {
tok := d.scanner.Next()
if len(tok) < 1 {
return nil, io.ErrUnexpectedEOF
}
switch tok[0] {
case '{':
d.state = (*Decoder).stateObjectString
d.push(true)
return tok, nil
case '[':
d.state = (*Decoder).stateArrayValue
d.push(false)
return tok, nil
case ']':
inObj := d.pop()
switch {
case d.len() == 0:
d.state = (*Decoder).stateEnd
case inObj:
d.state = (*Decoder).stateObjectComma
case !inObj:
d.state = (*Decoder).stateArrayComma
}
return tok, nil
case ',':
return nil, fmt.Errorf("stateArrayValue: unexpected comma")
default:
d.state = (*Decoder).stateArrayComma
return tok, nil
}
}
func (d *Decoder) stateArrayComma() ([]byte, error) {
tok := d.scanner.Next()
if len(tok) < 1 {
return nil, io.ErrUnexpectedEOF
}
switch tok[0] {
case ']':
inObj := d.pop()
switch {
case d.len() == 0:
d.state = (*Decoder).stateEnd
case inObj:
d.state = (*Decoder).stateObjectComma
case !inObj:
d.state = (*Decoder).stateArrayComma
}
return tok, nil
case Comma:
d.state = (*Decoder).stateArrayValue
return d.NextToken()
default:
return nil, fmt.Errorf("stateArrayComma: expected comma, %v", d.stack)
}
}
func (d *Decoder) stateValue() ([]byte, error) {
tok := d.scanner.Next()
if len(tok) < 1 {
return nil, io.ErrUnexpectedEOF
}
switch tok[0] {
case '{':
d.state = (*Decoder).stateObjectString
d.push(true)
return tok, nil
case '[':
d.state = (*Decoder).stateArrayValue
d.push(false)
return tok, nil
case ',':
return nil, fmt.Errorf("stateValue: unexpected comma")
default:
d.state = (*Decoder).stateEnd
return tok, nil
}
}
func (d *Decoder) stateEnd() ([]byte, error) { return nil, io.EOF }
// Decode reads the next JSON-encoded value from its input and stores it
// in the value pointed to by v.
func (d *Decoder) Decode(v interface{}) error {
rv := reflect.ValueOf(v)
switch {
case rv.Kind() != reflect.Ptr:
return fmt.Errorf("non-pointer %v", reflect.TypeOf(v))
case rv.IsNil():
return fmt.Errorf("nil")
default:
return d.decodeValue(rv.Elem())
}
}
func (d *Decoder) decodeValue(v reflect.Value) error {
tok, err := d.NextToken()
if err != nil {
return err
}
switch tok[0] {
case '{':
switch v.Kind() {
case reflect.Interface:
if v.NumMethod() > 0 {
return fmt.Errorf("cannot decode object into Go value of type %v", v.Type())
}
m, err := d.decodeMapAny()
if err != nil {
return err
}
v.Set(reflect.ValueOf(m))
case reflect.Map:
return d.decodeMap(v)
default:
return fmt.Errorf("decodeValue: unhandled type: %v", v.Kind())
}
return nil
case '[':
switch v.Kind() {
case reflect.Interface:
if v.NumMethod() > 0 {
return fmt.Errorf("cannot decode array into Go value of type %v", v.Type())
}
s, err := d.decodeSliceAny()
if err != nil {
return err
}
v.Set(reflect.ValueOf(s))
default:
return fmt.Errorf("unhandled type: %v", v.Kind())
}
return nil
case True, False:
value := tok[0] == 't'
switch v.Kind() {
case reflect.Bool:
v.SetBool(value)
case reflect.Interface:
if v.NumMethod() > 0 {
return fmt.Errorf("cannot decode bool into Go value of type %v", v.Type())
}
v.Set(reflect.ValueOf(value))
default:
return fmt.Errorf("unhandled type: %v", v.Kind())
}
return nil
case Null:
switch v.Kind() {
case reflect.Ptr, reflect.Map, reflect.Slice:
v.Set(reflect.Zero(v.Type()))
return nil
default:
return fmt.Errorf("unhandled type: %v", v.Kind())
}
case '"':
switch v.Kind() {
case reflect.Interface:
if v.NumMethod() > 0 {
return fmt.Errorf("cannot decode object into Go value of type %v", v.Type())
}
s := string(tok[1 : len(tok)-1])
v.Set(reflect.ValueOf(s))
case reflect.String:
s := string(tok[1 : len(tok)-1])
v.SetString(s)
default:
return fmt.Errorf("unhandled type: %v", v.Kind())
}
return nil
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
switch v.Kind() {
case reflect.Interface:
if v.NumMethod() > 0 {
return fmt.Errorf("cannot decode number into Go value of type %v", v.Type())
}
f, err := strconv.ParseFloat(bytesToString(tok), 64)
if err != nil {
return fmt.Errorf("cannot convert %q to float: %v", tok, err)
}
v.Set(reflect.ValueOf(f))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i, err := strconv.ParseInt(bytesToString(tok), 10, 64)
if err != nil || v.OverflowInt(i) {
return fmt.Errorf("cannot convert %q to int: %v", tok, err)
}
v.SetInt(i)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
u, err := strconv.ParseUint(bytesToString(tok), 10, 64)
if err != nil || v.OverflowUint(u) {
return fmt.Errorf("cannot convert %q to uint: %v", tok, err)
}
v.SetUint(u)
case reflect.Float64, reflect.Float32:
f, err := strconv.ParseFloat(bytesToString(tok), v.Type().Bits())
if err != nil || v.OverflowFloat(f) {
return fmt.Errorf("cannot convert %q to float: %v", tok, err)
}
v.SetFloat(f)
default:
return fmt.Errorf("unhandled type: %v", v.Kind())
}
return nil
default:
return fmt.Errorf("decodeValue: unhandled token: %c", tok[0])
}
}
func (d *Decoder) decodeValueAny() (interface{}, error) {
tok, err := d.NextToken()
if err != nil {
return nil, err
}
switch tok[0] {
case '{':
return d.decodeMapAny()
case '[':
return d.decodeSliceAny()
case True, False:
return tok[0] == 't', nil
case '"':
return string(tok[1 : len(tok)-1]), nil
case Null:
return nil, nil
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
s := bytesToString(tok)
return strconv.ParseFloat(s, 64)
default:
return fmt.Errorf("decodeValueAny: unhandled token: %c", tok[0]), nil
}
}
func (d *Decoder) decodeMapAny() (map[string]interface{}, error) {
m := make(map[string]interface{})
for {
tok, err := d.NextToken()
if err != nil {
return nil, err
}
if tok[0] == '}' {
return m, nil
}
key := string(tok[1 : len(tok)-1])
val, err := d.decodeValueAny()
if err != nil {
return nil, fmt.Errorf("decodeMapAny: %w", err)
}
m[key] = val
}
}
func (d *Decoder) decodeMap(v reflect.Value) error {
t := v.Type()
kt := t.Key()
if kt.Kind() != reflect.String {
return fmt.Errorf("cannot decode object into map with key type %v", kt)
}
for {
tok, err := d.NextToken()
if err != nil {
return err
}
if tok[0] == '}' {
return nil
}
key := string(tok[1 : len(tok)-1])
kv := reflect.ValueOf(key).Convert(kt)
value := reflect.New(t.Elem()).Elem()
if err := d.decodeValue(value); err != nil {
return err
}
v.SetMapIndex(kv, value)
}
}
func (d *Decoder) decodeSliceAny() ([]interface{}, error) {
s := make([]interface{}, 0, 1)
for {
tok, err := d.NextToken()
if err != nil {
return nil, err
}
switch tok[0] {
case ']':
return s, nil
case '{':
m, err := d.decodeMapAny()
if err != nil {
return nil, err
}
s = append(s, m)
case '[':
sv, err := d.decodeSliceAny()
if err != nil {
return nil, err
}
s = append(s, sv)
case True, False:
s = append(s, tok[0] == 't')
case '"':
s = append(s, string(tok[1:len(tok)-1]))
case Null:
s = append(s, nil)
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
ss := bytesToString(tok)
f, err := strconv.ParseFloat(ss, 64)
if err != nil {
return nil, fmt.Errorf("cannot convert %q to float: %v", tok, err)
}
s = append(s, f)
}
}
}
func bytesToString(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}