Debian Codegraph @

internal/decoder/array.go

raw
1 package decoder
2
3 import (
4 "fmt"
5 "reflect"
6 "unsafe"
7
8 "github.com/goccy/go-json/internal/errors"
9 )
10
11 type arrayDecoder struct {
12 elemType reflect.Type
13 // arrayPtrType is the type descriptor of the pointer to the array.
14 arrayPtrType unsafe.Pointer
15 size uintptr
16 valueDecoder Decoder
17 alen int
18 structName string
19 fieldName string
20 // typ is the type of the array, which the type errors report.
21 typ reflect.Type
22 }
23
24 func newArrayDecoder(dec Decoder, arrayType reflect.Type, structName, fieldName string) *arrayDecoder {
25 elemType := arrayType.Elem()
26 return &arrayDecoder{
27 typ: arrayType,
28 valueDecoder: dec,
29 elemType: elemType,
30 arrayPtrType: ptrTypeOf(arrayType),
31 size: elemType.Size(),
32 alen: arrayType.Len(),
33 structName: structName,
34 fieldName: fieldName,
35 }
36 }
37
38 // zeroFrom sets the elements of the array at p from idx to the end to their zero value,
39 // as encoding/json does for the elements the JSON array has not.
40 func (d *arrayDecoder) zeroFrom(p unsafe.Pointer, idx int) {
41 if idx < d.alen {
42 valueAt(d.arrayPtrType, p).Slice(idx, d.alen).Clear()
43 }
44 }
45
46 func (d *arrayDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
47 buf := ctx.Buf
48 depth++
49 if depth > maxDecodeNestingDepth {
50 return 0, errors.ErrExceededMaxDepth(buf[cursor], cursor)
51 }
52
53 for {
54 switch buf[cursor] {
55 case ' ', '\n', '\t', '\r':
56 cursor++
57 continue
58 case 'n':
59 if err := validateNull(buf, cursor); err != nil {
60 return 0, err
61 }
62 cursor += 4
63 return cursor, nil
64 case '[':
65 idx := 0
66 cursor++
67 cursor = skipWhiteSpace(buf, cursor)
68 if buf[cursor] == ']' {
69 d.zeroFrom(p, idx)
70 cursor++
71 return cursor, nil
72 }
73 for {
74 if idx < d.alen {
75 c, err := d.valueDecoder.Decode(ctx, cursor, depth, unsafe.Add(p, uintptr(idx)*d.size))
76 if err != nil {
77 return 0, err
78 }
79 cursor = c
80 } else {
81 c, err := skipValue(buf, cursor, depth)
82 if err != nil {
83 return 0, err
84 }
85 cursor = c
86 }
87 idx++
88 cursor = skipWhiteSpace(buf, cursor)
89 switch buf[cursor] {
90 case ']':
91 d.zeroFrom(p, idx)
92 cursor++
93 return cursor, nil
94 case ',':
95 cursor++
96 continue
97 default:
98 return 0, errors.ErrInvalidCharacter(buf[cursor], "array", cursor)
99 }
100 }
101 default:
102 return d.decodeOther(ctx, cursor, depth-1)
103 }
104 }
105 }
106
107 // decodeOther skips the value at cursor, which is not an array: a value of another kind is a type error, and
108 // anything else a syntax error. It is a function of its own, so that Decode keeps the size it had.
109 //
110 //go:noinline
111 func (d *arrayDecoder) decodeOther(ctx *RuntimeContext, cursor, depth int64) (int64, error) {
112 if isOtherValue(ctx.Buf[cursor], arrayValue) {
113 return ctx.skipTypeError(cursor, depth, d.typ)
114 }
115 return 0, errors.ErrUnexpectedEndOfJSON("array", cursor)
116 }
117
118 func (d *arrayDecoder) DecodePath(ctx *RuntimeContext, cursor, depth int64) ([][]byte, int64, error) {
119 return nil, 0, fmt.Errorf("json: array decoder does not support decode path")
120 }