decoder.go 5.3 KB

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