...
1
2
3
18
19 package decoder
20
21 import (
22 `bytes`
23 `encoding/json`
24 `io`
25 `reflect`
26 `unsafe`
27
28 `github.com/bytedance/sonic/internal/native/types`
29 `github.com/bytedance/sonic/option`
30 )
31
32 func init() {
33 println("WARNING: sonic only supports Go1.16~1.22 && CPU amd64, but your environment is not suitable")
34 }
35
36 const (
37 _F_use_int64 = 0
38 _F_disable_urc = 2
39 _F_disable_unknown = 3
40 _F_copy_string = 4
41
42 _F_use_number = types.B_USE_NUMBER
43 _F_validate_string = types.B_VALIDATE_STRING
44 _F_allow_control = types.B_ALLOW_CONTROL
45 )
46
47 type Options uint64
48
49 const (
50 OptionUseInt64 Options = 1 << _F_use_int64
51 OptionUseNumber Options = 1 << _F_use_number
52 OptionUseUnicodeErrors Options = 1 << _F_disable_urc
53 OptionDisableUnknown Options = 1 << _F_disable_unknown
54 OptionCopyString Options = 1 << _F_copy_string
55 OptionValidateString Options = 1 << _F_validate_string
56 )
57
58 func (self *Decoder) SetOptions(opts Options) {
59 if (opts & OptionUseNumber != 0) && (opts & OptionUseInt64 != 0) {
60 panic("can't set OptionUseInt64 and OptionUseNumber both!")
61 }
62 self.f = uint64(opts)
63 }
64
65
66
67 type Decoder struct {
68 i int
69 f uint64
70 s string
71 }
72
73
74 func NewDecoder(s string) *Decoder {
75 return &Decoder{s: s}
76 }
77
78
79 func (self *Decoder) Pos() int {
80 return self.i
81 }
82
83 func (self *Decoder) Reset(s string) {
84 self.s = s
85 self.i = 0
86
87 }
88
89
90 func (self *Decoder) CheckTrailings() error {
91 pos := self.i
92 buf := self.s
93
94 if pos != len(buf) {
95 for pos < len(buf) && (types.SPACE_MASK & (1 << buf[pos])) != 0 {
96 pos++
97 }
98 }
99
100
101 if pos == len(buf) {
102 return nil
103 }
104
105
106 return nil
107 }
108
109
110
111
112 func (self *Decoder) Decode(val interface{}) error {
113 r := bytes.NewBufferString(self.s)
114 dec := json.NewDecoder(r)
115 if (self.f & uint64(OptionUseNumber)) != 0 {
116 dec.UseNumber()
117 }
118 if (self.f & uint64(OptionDisableUnknown)) != 0 {
119 dec.DisallowUnknownFields()
120 }
121 return dec.Decode(val)
122 }
123
124
125
126 func (self *Decoder) UseInt64() {
127 self.f |= 1 << _F_use_int64
128 self.f &^= 1 << _F_use_number
129 }
130
131
132
133 func (self *Decoder) UseNumber() {
134 self.f &^= 1 << _F_use_int64
135 self.f |= 1 << _F_use_number
136 }
137
138
139
140 func (self *Decoder) UseUnicodeErrors() {
141 self.f |= 1 << _F_disable_urc
142 }
143
144
145
146
147 func (self *Decoder) DisallowUnknownFields() {
148 self.f |= 1 << _F_disable_unknown
149 }
150
151
152 func (self *Decoder) CopyString() {
153 self.f |= 1 << _F_copy_string
154 }
155
156
157
158
159 func (self *Decoder) ValidateString() {
160 self.f |= 1 << _F_validate_string
161 }
162
163
164
165
166
167
168 func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {
169 return nil
170 }
171
172 type StreamDecoder = json.Decoder
173
174
175
176
177 func NewStreamDecoder(r io.Reader) *StreamDecoder {
178 return json.NewDecoder(r)
179 }
180
181
182 type SyntaxError json.SyntaxError
183
184
185 func (s SyntaxError) Description() string {
186 return (*json.SyntaxError)(unsafe.Pointer(&s)).Error()
187 }
188
189 func (s SyntaxError) Error() string {
190 return (*json.SyntaxError)(unsafe.Pointer(&s)).Error()
191 }
192
193
194 type MismatchTypeError json.UnmarshalTypeError
View as plain text