encoder_compat.go 8.0 KB

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