github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/decoder/decoder_compat.go (about) 1 // +build !amd64 !go1.16 go1.23 2 3 /* 4 * Copyright 2023 ByteDance Inc. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 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 // Decoder is the decoder context object 67 type Decoder struct { 68 i int 69 f uint64 70 s string 71 } 72 73 // NewDecoder creates a new decoder instance. 74 func NewDecoder(s string) *Decoder { 75 return &Decoder{s: s} 76 } 77 78 // Pos returns the current decoding position. 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 // self.f = 0 87 } 88 89 // NOTE: api fallback do nothing 90 func (self *Decoder) CheckTrailings() error { 91 pos := self.i 92 buf := self.s 93 /* skip all the trailing spaces */ 94 if pos != len(buf) { 95 for pos < len(buf) && (types.SPACE_MASK & (1 << buf[pos])) != 0 { 96 pos++ 97 } 98 } 99 100 /* then it must be at EOF */ 101 if pos == len(buf) { 102 return nil 103 } 104 105 /* junk after JSON value */ 106 return nil 107 } 108 109 110 // Decode parses the JSON-encoded data from current position and stores the result 111 // in the value pointed to by val. 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 // UseInt64 indicates the Decoder to unmarshal an integer into an interface{} as an 125 // int64 instead of as a float64. 126 func (self *Decoder) UseInt64() { 127 self.f |= 1 << _F_use_int64 128 self.f &^= 1 << _F_use_number 129 } 130 131 // UseNumber indicates the Decoder to unmarshal a number into an interface{} as a 132 // json.Number instead of as a float64. 133 func (self *Decoder) UseNumber() { 134 self.f &^= 1 << _F_use_int64 135 self.f |= 1 << _F_use_number 136 } 137 138 // UseUnicodeErrors indicates the Decoder to return an error when encounter invalid 139 // UTF-8 escape sequences. 140 func (self *Decoder) UseUnicodeErrors() { 141 self.f |= 1 << _F_disable_urc 142 } 143 144 // DisallowUnknownFields indicates the Decoder to return an error when the destination 145 // is a struct and the input contains object keys which do not match any 146 // non-ignored, exported fields in the destination. 147 func (self *Decoder) DisallowUnknownFields() { 148 self.f |= 1 << _F_disable_unknown 149 } 150 151 // CopyString indicates the Decoder to decode string values by copying instead of referring. 152 func (self *Decoder) CopyString() { 153 self.f |= 1 << _F_copy_string 154 } 155 156 // ValidateString causes the Decoder to validate string values when decoding string value 157 // in JSON. Validation is that, returning error when unescaped control chars(0x00-0x1f) or 158 // invalid UTF-8 chars in the string value of JSON. 159 func (self *Decoder) ValidateString() { 160 self.f |= 1 << _F_validate_string 161 } 162 163 // Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in 164 // order to reduce the first-hit latency. 165 // 166 // Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is 167 // a compile option to set the depth of recursive compile for the nested struct type. 168 func Pretouch(vt reflect.Type, opts ...option.CompileOption) error { 169 return nil 170 } 171 172 type StreamDecoder = json.Decoder 173 174 // NewStreamDecoder adapts to encoding/json.NewDecoder API. 175 // 176 // NewStreamDecoder returns a new decoder that reads from r. 177 func NewStreamDecoder(r io.Reader) *StreamDecoder { 178 return json.NewDecoder(r) 179 } 180 181 // SyntaxError represents json syntax error 182 type SyntaxError json.SyntaxError 183 184 // Description 185 func (s SyntaxError) Description() string { 186 return (*json.SyntaxError)(unsafe.Pointer(&s)).Error() 187 } 188 // Error 189 func (s SyntaxError) Error() string { 190 return (*json.SyntaxError)(unsafe.Pointer(&s)).Error() 191 } 192 193 // MismatchTypeError represents dismatching between json and object 194 type MismatchTypeError json.UnmarshalTypeError