本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

391 lines
9.7KB

  1. // Package jwriter contains a JSON writer.
  2. package jwriter
  3. import (
  4. "io"
  5. "strconv"
  6. "unicode/utf8"
  7. "github.com/mailru/easyjson/buffer"
  8. )
  9. // Flags describe various encoding options. The behavior may be actually implemented in the encoder, but
  10. // Flags field in Writer is used to set and pass them around.
  11. type Flags int
  12. const (
  13. NilMapAsEmpty Flags = 1 << iota // Encode nil map as '{}' rather than 'null'.
  14. NilSliceAsEmpty // Encode nil slice as '[]' rather than 'null'.
  15. )
  16. // Writer is a JSON writer.
  17. type Writer struct {
  18. Flags Flags
  19. Error error
  20. Buffer buffer.Buffer
  21. NoEscapeHTML bool
  22. }
  23. // Size returns the size of the data that was written out.
  24. func (w *Writer) Size() int {
  25. return w.Buffer.Size()
  26. }
  27. // DumpTo outputs the data to given io.Writer, resetting the buffer.
  28. func (w *Writer) DumpTo(out io.Writer) (written int, err error) {
  29. return w.Buffer.DumpTo(out)
  30. }
  31. // BuildBytes returns writer data as a single byte slice. You can optionally provide one byte slice
  32. // as argument that it will try to reuse.
  33. func (w *Writer) BuildBytes(reuse ...[]byte) ([]byte, error) {
  34. if w.Error != nil {
  35. return nil, w.Error
  36. }
  37. return w.Buffer.BuildBytes(reuse...), nil
  38. }
  39. // ReadCloser returns an io.ReadCloser that can be used to read the data.
  40. // ReadCloser also resets the buffer.
  41. func (w *Writer) ReadCloser() (io.ReadCloser, error) {
  42. if w.Error != nil {
  43. return nil, w.Error
  44. }
  45. return w.Buffer.ReadCloser(), nil
  46. }
  47. // RawByte appends raw binary data to the buffer.
  48. func (w *Writer) RawByte(c byte) {
  49. w.Buffer.AppendByte(c)
  50. }
  51. // RawByte appends raw binary data to the buffer.
  52. func (w *Writer) RawString(s string) {
  53. w.Buffer.AppendString(s)
  54. }
  55. // Raw appends raw binary data to the buffer or sets the error if it is given. Useful for
  56. // calling with results of MarshalJSON-like functions.
  57. func (w *Writer) Raw(data []byte, err error) {
  58. switch {
  59. case w.Error != nil:
  60. return
  61. case err != nil:
  62. w.Error = err
  63. case len(data) > 0:
  64. w.Buffer.AppendBytes(data)
  65. default:
  66. w.RawString("null")
  67. }
  68. }
  69. // RawText encloses raw binary data in quotes and appends in to the buffer.
  70. // Useful for calling with results of MarshalText-like functions.
  71. func (w *Writer) RawText(data []byte, err error) {
  72. switch {
  73. case w.Error != nil:
  74. return
  75. case err != nil:
  76. w.Error = err
  77. case len(data) > 0:
  78. w.String(string(data))
  79. default:
  80. w.RawString("null")
  81. }
  82. }
  83. // Base64Bytes appends data to the buffer after base64 encoding it
  84. func (w *Writer) Base64Bytes(data []byte) {
  85. if data == nil {
  86. w.Buffer.AppendString("null")
  87. return
  88. }
  89. w.Buffer.AppendByte('"')
  90. w.base64(data)
  91. w.Buffer.AppendByte('"')
  92. }
  93. func (w *Writer) Uint8(n uint8) {
  94. w.Buffer.EnsureSpace(3)
  95. w.Buffer.Buf = strconv.AppendUint(w.Buffer.Buf, uint64(n), 10)
  96. }
  97. func (w *Writer) Uint16(n uint16) {
  98. w.Buffer.EnsureSpace(5)
  99. w.Buffer.Buf = strconv.AppendUint(w.Buffer.Buf, uint64(n), 10)
  100. }
  101. func (w *Writer) Uint32(n uint32) {
  102. w.Buffer.EnsureSpace(10)
  103. w.Buffer.Buf = strconv.AppendUint(w.Buffer.Buf, uint64(n), 10)
  104. }
  105. func (w *Writer) Uint(n uint) {
  106. w.Buffer.EnsureSpace(20)
  107. w.Buffer.Buf = strconv.AppendUint(w.Buffer.Buf, uint64(n), 10)
  108. }
  109. func (w *Writer) Uint64(n uint64) {
  110. w.Buffer.EnsureSpace(20)
  111. w.Buffer.Buf = strconv.AppendUint(w.Buffer.Buf, n, 10)
  112. }
  113. func (w *Writer) Int8(n int8) {
  114. w.Buffer.EnsureSpace(4)
  115. w.Buffer.Buf = strconv.AppendInt(w.Buffer.Buf, int64(n), 10)
  116. }
  117. func (w *Writer) Int16(n int16) {
  118. w.Buffer.EnsureSpace(6)
  119. w.Buffer.Buf = strconv.AppendInt(w.Buffer.Buf, int64(n), 10)
  120. }
  121. func (w *Writer) Int32(n int32) {
  122. w.Buffer.EnsureSpace(11)
  123. w.Buffer.Buf = strconv.AppendInt(w.Buffer.Buf, int64(n), 10)
  124. }
  125. func (w *Writer) Int(n int) {
  126. w.Buffer.EnsureSpace(21)
  127. w.Buffer.Buf = strconv.AppendInt(w.Buffer.Buf, int64(n), 10)
  128. }
  129. func (w *Writer) Int64(n int64) {
  130. w.Buffer.EnsureSpace(21)
  131. w.Buffer.Buf = strconv.AppendInt(w.Buffer.Buf, n, 10)
  132. }
  133. func (w *Writer) Uint8Str(n uint8) {
  134. w.Buffer.EnsureSpace(3)
  135. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  136. w.Buffer.Buf = strconv.AppendUint(w.Buffer.Buf, uint64(n), 10)
  137. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  138. }
  139. func (w *Writer) Uint16Str(n uint16) {
  140. w.Buffer.EnsureSpace(5)
  141. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  142. w.Buffer.Buf = strconv.AppendUint(w.Buffer.Buf, uint64(n), 10)
  143. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  144. }
  145. func (w *Writer) Uint32Str(n uint32) {
  146. w.Buffer.EnsureSpace(10)
  147. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  148. w.Buffer.Buf = strconv.AppendUint(w.Buffer.Buf, uint64(n), 10)
  149. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  150. }
  151. func (w *Writer) UintStr(n uint) {
  152. w.Buffer.EnsureSpace(20)
  153. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  154. w.Buffer.Buf = strconv.AppendUint(w.Buffer.Buf, uint64(n), 10)
  155. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  156. }
  157. func (w *Writer) Uint64Str(n uint64) {
  158. w.Buffer.EnsureSpace(20)
  159. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  160. w.Buffer.Buf = strconv.AppendUint(w.Buffer.Buf, n, 10)
  161. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  162. }
  163. func (w *Writer) UintptrStr(n uintptr) {
  164. w.Buffer.EnsureSpace(20)
  165. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  166. w.Buffer.Buf = strconv.AppendUint(w.Buffer.Buf, uint64(n), 10)
  167. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  168. }
  169. func (w *Writer) Int8Str(n int8) {
  170. w.Buffer.EnsureSpace(4)
  171. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  172. w.Buffer.Buf = strconv.AppendInt(w.Buffer.Buf, int64(n), 10)
  173. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  174. }
  175. func (w *Writer) Int16Str(n int16) {
  176. w.Buffer.EnsureSpace(6)
  177. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  178. w.Buffer.Buf = strconv.AppendInt(w.Buffer.Buf, int64(n), 10)
  179. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  180. }
  181. func (w *Writer) Int32Str(n int32) {
  182. w.Buffer.EnsureSpace(11)
  183. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  184. w.Buffer.Buf = strconv.AppendInt(w.Buffer.Buf, int64(n), 10)
  185. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  186. }
  187. func (w *Writer) IntStr(n int) {
  188. w.Buffer.EnsureSpace(21)
  189. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  190. w.Buffer.Buf = strconv.AppendInt(w.Buffer.Buf, int64(n), 10)
  191. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  192. }
  193. func (w *Writer) Int64Str(n int64) {
  194. w.Buffer.EnsureSpace(21)
  195. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  196. w.Buffer.Buf = strconv.AppendInt(w.Buffer.Buf, n, 10)
  197. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  198. }
  199. func (w *Writer) Float32(n float32) {
  200. w.Buffer.EnsureSpace(20)
  201. w.Buffer.Buf = strconv.AppendFloat(w.Buffer.Buf, float64(n), 'g', -1, 32)
  202. }
  203. func (w *Writer) Float32Str(n float32) {
  204. w.Buffer.EnsureSpace(20)
  205. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  206. w.Buffer.Buf = strconv.AppendFloat(w.Buffer.Buf, float64(n), 'g', -1, 32)
  207. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  208. }
  209. func (w *Writer) Float64(n float64) {
  210. w.Buffer.EnsureSpace(20)
  211. w.Buffer.Buf = strconv.AppendFloat(w.Buffer.Buf, n, 'g', -1, 64)
  212. }
  213. func (w *Writer) Float64Str(n float64) {
  214. w.Buffer.EnsureSpace(20)
  215. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  216. w.Buffer.Buf = strconv.AppendFloat(w.Buffer.Buf, float64(n), 'g', -1, 64)
  217. w.Buffer.Buf = append(w.Buffer.Buf, '"')
  218. }
  219. func (w *Writer) Bool(v bool) {
  220. w.Buffer.EnsureSpace(5)
  221. if v {
  222. w.Buffer.Buf = append(w.Buffer.Buf, "true"...)
  223. } else {
  224. w.Buffer.Buf = append(w.Buffer.Buf, "false"...)
  225. }
  226. }
  227. const chars = "0123456789abcdef"
  228. func isNotEscapedSingleChar(c byte, escapeHTML bool) bool {
  229. // Note: might make sense to use a table if there are more chars to escape. With 4 chars
  230. // it benchmarks the same.
  231. if escapeHTML {
  232. return c != '<' && c != '>' && c != '&' && c != '\\' && c != '"' && c >= 0x20 && c < utf8.RuneSelf
  233. } else {
  234. return c != '\\' && c != '"' && c >= 0x20 && c < utf8.RuneSelf
  235. }
  236. }
  237. func (w *Writer) String(s string) {
  238. w.Buffer.AppendByte('"')
  239. // Portions of the string that contain no escapes are appended as
  240. // byte slices.
  241. p := 0 // last non-escape symbol
  242. for i := 0; i < len(s); {
  243. c := s[i]
  244. if isNotEscapedSingleChar(c, !w.NoEscapeHTML) {
  245. // single-width character, no escaping is required
  246. i++
  247. continue
  248. } else if c < utf8.RuneSelf {
  249. // single-with character, need to escape
  250. w.Buffer.AppendString(s[p:i])
  251. switch c {
  252. case '\t':
  253. w.Buffer.AppendString(`\t`)
  254. case '\r':
  255. w.Buffer.AppendString(`\r`)
  256. case '\n':
  257. w.Buffer.AppendString(`\n`)
  258. case '\\':
  259. w.Buffer.AppendString(`\\`)
  260. case '"':
  261. w.Buffer.AppendString(`\"`)
  262. default:
  263. w.Buffer.AppendString(`\u00`)
  264. w.Buffer.AppendByte(chars[c>>4])
  265. w.Buffer.AppendByte(chars[c&0xf])
  266. }
  267. i++
  268. p = i
  269. continue
  270. }
  271. // broken utf
  272. runeValue, runeWidth := utf8.DecodeRuneInString(s[i:])
  273. if runeValue == utf8.RuneError && runeWidth == 1 {
  274. w.Buffer.AppendString(s[p:i])
  275. w.Buffer.AppendString(`\ufffd`)
  276. i++
  277. p = i
  278. continue
  279. }
  280. // jsonp stuff - tab separator and line separator
  281. if runeValue == '\u2028' || runeValue == '\u2029' {
  282. w.Buffer.AppendString(s[p:i])
  283. w.Buffer.AppendString(`\u202`)
  284. w.Buffer.AppendByte(chars[runeValue&0xf])
  285. i += runeWidth
  286. p = i
  287. continue
  288. }
  289. i += runeWidth
  290. }
  291. w.Buffer.AppendString(s[p:])
  292. w.Buffer.AppendByte('"')
  293. }
  294. const encode = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  295. const padChar = '='
  296. func (w *Writer) base64(in []byte) {
  297. if len(in) == 0 {
  298. return
  299. }
  300. w.Buffer.EnsureSpace(((len(in)-1)/3 + 1) * 4)
  301. si := 0
  302. n := (len(in) / 3) * 3
  303. for si < n {
  304. // Convert 3x 8bit source bytes into 4 bytes
  305. val := uint(in[si+0])<<16 | uint(in[si+1])<<8 | uint(in[si+2])
  306. w.Buffer.Buf = append(w.Buffer.Buf, encode[val>>18&0x3F], encode[val>>12&0x3F], encode[val>>6&0x3F], encode[val&0x3F])
  307. si += 3
  308. }
  309. remain := len(in) - si
  310. if remain == 0 {
  311. return
  312. }
  313. // Add the remaining small block
  314. val := uint(in[si+0]) << 16
  315. if remain == 2 {
  316. val |= uint(in[si+1]) << 8
  317. }
  318. w.Buffer.Buf = append(w.Buffer.Buf, encode[val>>18&0x3F], encode[val>>12&0x3F])
  319. switch remain {
  320. case 2:
  321. w.Buffer.Buf = append(w.Buffer.Buf, encode[val>>6&0x3F], byte(padChar))
  322. case 1:
  323. w.Buffer.Buf = append(w.Buffer.Buf, byte(padChar), byte(padChar))
  324. }
  325. }
上海开阖软件有限公司 沪ICP备12045867号-1