decoder_compat.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // +build !amd64 !go1.16 go1.22
  2. /*
  3. * Copyright 2023 ByteDance Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package decoder
  18. import (
  19. `bytes`
  20. `encoding/json`
  21. `io`
  22. `reflect`
  23. `unsafe`
  24. `github.com/bytedance/sonic/internal/native/types`
  25. `github.com/bytedance/sonic/option`
  26. )
  27. func init() {
  28. println("WARNING: sonic only supports Go1.16~1.20 && CPU amd64, but your environment is not suitable")
  29. }
  30. const (
  31. _F_use_int64 = 0
  32. _F_disable_urc = 2
  33. _F_disable_unknown = 3
  34. _F_copy_string = 4
  35. _F_use_number = types.B_USE_NUMBER
  36. _F_validate_string = types.B_VALIDATE_STRING
  37. _F_allow_control = types.B_ALLOW_CONTROL
  38. )
  39. type Options uint64
  40. const (
  41. OptionUseInt64 Options = 1 << _F_use_int64
  42. OptionUseNumber Options = 1 << _F_use_number
  43. OptionUseUnicodeErrors Options = 1 << _F_disable_urc
  44. OptionDisableUnknown Options = 1 << _F_disable_unknown
  45. OptionCopyString Options = 1 << _F_copy_string
  46. OptionValidateString Options = 1 << _F_validate_string
  47. )
  48. func (self *Decoder) SetOptions(opts Options) {
  49. if (opts & OptionUseNumber != 0) && (opts & OptionUseInt64 != 0) {
  50. panic("can't set OptionUseInt64 and OptionUseNumber both!")
  51. }
  52. self.f = uint64(opts)
  53. }
  54. // Decoder is the decoder context object
  55. type Decoder struct {
  56. i int
  57. f uint64
  58. s string
  59. }
  60. // NewDecoder creates a new decoder instance.
  61. func NewDecoder(s string) *Decoder {
  62. return &Decoder{s: s}
  63. }
  64. // Pos returns the current decoding position.
  65. func (self *Decoder) Pos() int {
  66. return self.i
  67. }
  68. func (self *Decoder) Reset(s string) {
  69. self.s = s
  70. self.i = 0
  71. // self.f = 0
  72. }
  73. // NOTE: api fallback do nothing
  74. func (self *Decoder) CheckTrailings() error {
  75. pos := self.i
  76. buf := self.s
  77. /* skip all the trailing spaces */
  78. if pos != len(buf) {
  79. for pos < len(buf) && (types.SPACE_MASK & (1 << buf[pos])) != 0 {
  80. pos++
  81. }
  82. }
  83. /* then it must be at EOF */
  84. if pos == len(buf) {
  85. return nil
  86. }
  87. /* junk after JSON value */
  88. return nil
  89. }
  90. // Decode parses the JSON-encoded data from current position and stores the result
  91. // in the value pointed to by val.
  92. func (self *Decoder) Decode(val interface{}) error {
  93. r := bytes.NewBufferString(self.s)
  94. dec := json.NewDecoder(r)
  95. if (self.f & uint64(OptionUseNumber)) != 0 {
  96. dec.UseNumber()
  97. }
  98. if (self.f & uint64(OptionDisableUnknown)) != 0 {
  99. dec.DisallowUnknownFields()
  100. }
  101. return dec.Decode(val)
  102. }
  103. // UseInt64 indicates the Decoder to unmarshal an integer into an interface{} as an
  104. // int64 instead of as a float64.
  105. func (self *Decoder) UseInt64() {
  106. self.f |= 1 << _F_use_int64
  107. self.f &^= 1 << _F_use_number
  108. }
  109. // UseNumber indicates the Decoder to unmarshal a number into an interface{} as a
  110. // json.Number instead of as a float64.
  111. func (self *Decoder) UseNumber() {
  112. self.f &^= 1 << _F_use_int64
  113. self.f |= 1 << _F_use_number
  114. }
  115. // UseUnicodeErrors indicates the Decoder to return an error when encounter invalid
  116. // UTF-8 escape sequences.
  117. func (self *Decoder) UseUnicodeErrors() {
  118. self.f |= 1 << _F_disable_urc
  119. }
  120. // DisallowUnknownFields indicates the Decoder to return an error when the destination
  121. // is a struct and the input contains object keys which do not match any
  122. // non-ignored, exported fields in the destination.
  123. func (self *Decoder) DisallowUnknownFields() {
  124. self.f |= 1 << _F_disable_unknown
  125. }
  126. // CopyString indicates the Decoder to decode string values by copying instead of referring.
  127. func (self *Decoder) CopyString() {
  128. self.f |= 1 << _F_copy_string
  129. }
  130. // ValidateString causes the Decoder to validate string values when decoding string value
  131. // in JSON. Validation is that, returning error when unescaped control chars(0x00-0x1f) or
  132. // invalid UTF-8 chars in the string value of JSON.
  133. func (self *Decoder) ValidateString() {
  134. self.f |= 1 << _F_validate_string
  135. }
  136. // Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in
  137. // order to reduce the first-hit latency.
  138. //
  139. // Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is
  140. // a compile option to set the depth of recursive compile for the nested struct type.
  141. func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {
  142. return nil
  143. }
  144. type StreamDecoder = json.Decoder
  145. // NewStreamDecoder adapts to encoding/json.NewDecoder API.
  146. //
  147. // NewStreamDecoder returns a new decoder that reads from r.
  148. func NewStreamDecoder(r io.Reader) *StreamDecoder {
  149. return json.NewDecoder(r)
  150. }
  151. // SyntaxError represents json syntax error
  152. type SyntaxError json.SyntaxError
  153. // Description
  154. func (s SyntaxError) Description() string {
  155. return (*json.SyntaxError)(unsafe.Pointer(&s)).Error()
  156. }
  157. // Error
  158. func (s SyntaxError) Error() string {
  159. return (*json.SyntaxError)(unsafe.Pointer(&s)).Error()
  160. }
  161. // MismatchTypeError represents dismatching between json and object
  162. type MismatchTypeError json.UnmarshalTypeError