encoder_compat.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 encoder
  18. import (
  19. `io`
  20. `bytes`
  21. `encoding/json`
  22. `reflect`
  23. `github.com/bytedance/sonic/option`
  24. )
  25. func init() {
  26. println("WARNING: sonic only supports Go1.16~1.20 && CPU amd64, but your environment is not suitable")
  27. }
  28. // Options is a set of encoding options.
  29. type Options uint64
  30. const (
  31. bitSortMapKeys = iota
  32. bitEscapeHTML
  33. bitCompactMarshaler
  34. bitNoQuoteTextMarshaler
  35. bitNoNullSliceOrMap
  36. bitValidateString
  37. bitNoValidateJSONMarshaler
  38. // used for recursive compile
  39. bitPointerValue = 63
  40. )
  41. const (
  42. // SortMapKeys indicates that the keys of a map needs to be sorted
  43. // before serializing into JSON.
  44. // WARNING: This hurts performance A LOT, USE WITH CARE.
  45. SortMapKeys Options = 1 << bitSortMapKeys
  46. // EscapeHTML indicates encoder to escape all HTML characters
  47. // after serializing into JSON (see https://pkg.go.dev/encoding/json#HTMLEscape).
  48. // WARNING: This hurts performance A LOT, USE WITH CARE.
  49. EscapeHTML Options = 1 << bitEscapeHTML
  50. // CompactMarshaler indicates that the output JSON from json.Marshaler
  51. // is always compact and needs no validation
  52. CompactMarshaler Options = 1 << bitCompactMarshaler
  53. // NoQuoteTextMarshaler indicates that the output text from encoding.TextMarshaler
  54. // is always escaped string and needs no quoting
  55. NoQuoteTextMarshaler Options = 1 << bitNoQuoteTextMarshaler
  56. // NoNullSliceOrMap indicates all empty Array or Object are encoded as '[]' or '{}',
  57. // instead of 'null'
  58. NoNullSliceOrMap Options = 1 << bitNoNullSliceOrMap
  59. // ValidateString indicates that encoder should validate the input string
  60. // before encoding it into JSON.
  61. ValidateString Options = 1 << bitValidateString
  62. // NoValidateJSONMarshaler indicates that the encoder should not validate the output string
  63. // after encoding the JSONMarshaler to JSON.
  64. NoValidateJSONMarshaler Options = 1 << bitNoValidateJSONMarshaler
  65. // CompatibleWithStd is used to be compatible with std encoder.
  66. CompatibleWithStd Options = SortMapKeys | EscapeHTML | CompactMarshaler
  67. )
  68. // Encoder represents a specific set of encoder configurations.
  69. type Encoder struct {
  70. Opts Options
  71. prefix string
  72. indent string
  73. }
  74. // Encode returns the JSON encoding of v.
  75. func (self *Encoder) Encode(v interface{}) ([]byte, error) {
  76. if self.indent != "" || self.prefix != "" {
  77. return EncodeIndented(v, self.prefix, self.indent, self.Opts)
  78. }
  79. return Encode(v, self.Opts)
  80. }
  81. // SortKeys enables the SortMapKeys option.
  82. func (self *Encoder) SortKeys() *Encoder {
  83. self.Opts |= SortMapKeys
  84. return self
  85. }
  86. // SetEscapeHTML specifies if option EscapeHTML opens
  87. func (self *Encoder) SetEscapeHTML(f bool) {
  88. if f {
  89. self.Opts |= EscapeHTML
  90. } else {
  91. self.Opts &= ^EscapeHTML
  92. }
  93. }
  94. // SetValidateString specifies if option ValidateString opens
  95. func (self *Encoder) SetValidateString(f bool) {
  96. if f {
  97. self.Opts |= ValidateString
  98. } else {
  99. self.Opts &= ^ValidateString
  100. }
  101. }
  102. // SetNoValidateJSONMarshaler specifies if option NoValidateJSONMarshaler opens
  103. func (self *Encoder) SetNoValidateJSONMarshaler(f bool) {
  104. if f {
  105. self.Opts |= NoValidateJSONMarshaler
  106. } else {
  107. self.Opts &= ^NoValidateJSONMarshaler
  108. }
  109. }
  110. // SetCompactMarshaler specifies if option CompactMarshaler opens
  111. func (self *Encoder) SetCompactMarshaler(f bool) {
  112. if f {
  113. self.Opts |= CompactMarshaler
  114. } else {
  115. self.Opts &= ^CompactMarshaler
  116. }
  117. }
  118. // SetNoQuoteTextMarshaler specifies if option NoQuoteTextMarshaler opens
  119. func (self *Encoder) SetNoQuoteTextMarshaler(f bool) {
  120. if f {
  121. self.Opts |= NoQuoteTextMarshaler
  122. } else {
  123. self.Opts &= ^NoQuoteTextMarshaler
  124. }
  125. }
  126. // SetIndent instructs the encoder to format each subsequent encoded
  127. // value as if indented by the package-level function EncodeIndent().
  128. // Calling SetIndent("", "") disables indentation.
  129. func (enc *Encoder) SetIndent(prefix, indent string) {
  130. enc.prefix = prefix
  131. enc.indent = indent
  132. }
  133. // Quote returns the JSON-quoted version of s.
  134. func Quote(s string) string {
  135. /* check for empty string */
  136. if s == "" {
  137. return `""`
  138. }
  139. out, _ := json.Marshal(s)
  140. return string(out)
  141. }
  142. // Encode returns the JSON encoding of val, encoded with opts.
  143. func Encode(val interface{}, opts Options) ([]byte, error) {
  144. return json.Marshal(val)
  145. }
  146. // EncodeInto is like Encode but uses a user-supplied buffer instead of allocating
  147. // a new one.
  148. func EncodeInto(buf *[]byte, val interface{}, opts Options) error {
  149. if buf == nil {
  150. panic("user-supplied buffer buf is nil")
  151. }
  152. w := bytes.NewBuffer(*buf)
  153. enc := json.NewEncoder(w)
  154. enc.SetEscapeHTML((opts & EscapeHTML) != 0)
  155. err := enc.Encode(val)
  156. *buf = w.Bytes()
  157. return err
  158. }
  159. // HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
  160. // characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
  161. // so that the JSON will be safe to embed inside HTML <script> tags.
  162. // For historical reasons, web browsers don't honor standard HTML
  163. // escaping within <script> tags, so an alternative JSON encoding must
  164. // be used.
  165. func HTMLEscape(dst []byte, src []byte) []byte {
  166. d := bytes.NewBuffer(dst)
  167. json.HTMLEscape(d, src)
  168. return d.Bytes()
  169. }
  170. // EncodeIndented is like Encode but applies Indent to format the output.
  171. // Each JSON element in the output will begin on a new line beginning with prefix
  172. // followed by one or more copies of indent according to the indentation nesting.
  173. func EncodeIndented(val interface{}, prefix string, indent string, opts Options) ([]byte, error) {
  174. w := bytes.NewBuffer([]byte{})
  175. enc := json.NewEncoder(w)
  176. enc.SetEscapeHTML((opts & EscapeHTML) != 0)
  177. enc.SetIndent(prefix, indent)
  178. err := enc.Encode(val)
  179. out := w.Bytes()
  180. return out, err
  181. }
  182. // Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in
  183. // order to reduce the first-hit latency.
  184. //
  185. // Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is
  186. // a compile option to set the depth of recursive compile for the nested struct type.
  187. func Pretouch(vt reflect.Type, opts ...option.CompileOption) error {
  188. return nil
  189. }
  190. // Valid validates json and returns first non-blank character position,
  191. // if it is only one valid json value.
  192. // Otherwise returns invalid character position using start.
  193. //
  194. // Note: it does not check for the invalid UTF-8 characters.
  195. func Valid(data []byte) (ok bool, start int) {
  196. return json.Valid(data), 0
  197. }
  198. // StreamEncoder uses io.Writer as
  199. type StreamEncoder = json.Encoder
  200. // NewStreamEncoder adapts to encoding/json.NewDecoder API.
  201. //
  202. // NewStreamEncoder returns a new encoder that write to w.
  203. func NewStreamEncoder(w io.Writer) *StreamEncoder {
  204. return json.NewEncoder(w)
  205. }