encoder_compat.go 8.0 KB

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