-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
decode_struct.go
133 lines (127 loc) · 2.7 KB
/
decode_struct.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
package json
import (
"fmt"
"unsafe"
)
type structFieldSet struct {
dec decoder
offset uintptr
}
type structDecoder struct {
fieldMap map[string]*structFieldSet
keyDecoder *stringDecoder
}
func newStructDecoder(fieldMap map[string]*structFieldSet) *structDecoder {
return &structDecoder{
fieldMap: fieldMap,
keyDecoder: newStringDecoder(),
}
}
func (d *structDecoder) decodeStream(s *stream, p uintptr) error {
s.skipWhiteSpace()
if s.char() == nul {
s.read()
}
if s.char() != '{' {
return errNotAtBeginningOfValue(s.totalOffset())
}
s.cursor++
for {
s.reset()
key, err := d.keyDecoder.decodeStreamByte(s)
if err != nil {
return err
}
s.skipWhiteSpace()
if s.char() == nul {
s.read()
}
if s.char() != ':' {
return errExpected("colon after object key", s.totalOffset())
}
s.cursor++
if s.char() == nul {
s.read()
}
if s.end() {
return errExpected("object value after colon", s.totalOffset())
}
k := *(*string)(unsafe.Pointer(&key))
field, exists := d.fieldMap[k]
if exists {
if err := field.dec.decodeStream(s, p+field.offset); err != nil {
return err
}
} else if s.disallowUnknownFields {
return fmt.Errorf("json: unknown field %q", k)
} else {
if err := s.skipValue(); err != nil {
return err
}
}
s.skipWhiteSpace()
if s.char() == nul {
s.read()
}
c := s.char()
if c == '}' {
s.cursor++
return nil
}
if c != ',' {
return errExpected("comma after object element", s.totalOffset())
}
s.cursor++
}
return nil
}
func (d *structDecoder) decode(buf []byte, cursor int64, p uintptr) (int64, error) {
buflen := int64(len(buf))
cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] != '{' {
return 0, errNotAtBeginningOfValue(cursor)
}
if buflen < 2 {
return 0, errUnexpectedEndOfJSON("object", cursor)
}
cursor++
for ; cursor < buflen; cursor++ {
key, c, err := d.keyDecoder.decodeByte(buf, cursor)
if err != nil {
return 0, err
}
cursor = c
cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] != ':' {
return 0, errExpected("colon after object key", cursor)
}
cursor++
if cursor >= buflen {
return 0, errExpected("object value after colon", cursor)
}
k := *(*string)(unsafe.Pointer(&key))
field, exists := d.fieldMap[k]
if exists {
c, err := field.dec.decode(buf, cursor, p+field.offset)
if err != nil {
return 0, err
}
cursor = c
} else {
c, err := skipValue(buf, cursor)
if err != nil {
return 0, err
}
cursor = c
}
cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] == '}' {
cursor++
return cursor, nil
}
if buf[cursor] != ',' {
return 0, errExpected("comma after object element", cursor)
}
}
return cursor, nil
}