json_bytes.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package msgp
  2. import (
  3. "bufio"
  4. "encoding/base64"
  5. "encoding/json"
  6. "io"
  7. "strconv"
  8. "time"
  9. )
  10. var unfuns [_maxtype]func(jsWriter, []byte, []byte, int) ([]byte, []byte, error)
  11. func init() {
  12. // NOTE(pmh): this is best expressed as a jump table,
  13. // but gc doesn't do that yet. revisit post-go1.5.
  14. unfuns = [_maxtype]func(jsWriter, []byte, []byte, int) ([]byte, []byte, error){
  15. StrType: rwStringBytes,
  16. BinType: rwBytesBytes,
  17. MapType: rwMapBytes,
  18. ArrayType: rwArrayBytes,
  19. Float64Type: rwFloat64Bytes,
  20. Float32Type: rwFloat32Bytes,
  21. BoolType: rwBoolBytes,
  22. IntType: rwIntBytes,
  23. UintType: rwUintBytes,
  24. NilType: rwNullBytes,
  25. ExtensionType: rwExtensionBytes,
  26. Complex64Type: rwExtensionBytes,
  27. Complex128Type: rwExtensionBytes,
  28. TimeType: rwTimeBytes,
  29. }
  30. }
  31. // UnmarshalAsJSON takes raw messagepack and writes
  32. // it as JSON to 'w'. If an error is returned, the
  33. // bytes not translated will also be returned. If
  34. // no errors are encountered, the length of the returned
  35. // slice will be zero.
  36. func UnmarshalAsJSON(w io.Writer, msg []byte) ([]byte, error) {
  37. var (
  38. scratch []byte
  39. cast bool
  40. dst jsWriter
  41. err error
  42. )
  43. if jsw, ok := w.(jsWriter); ok {
  44. dst = jsw
  45. cast = true
  46. } else {
  47. dst = bufio.NewWriterSize(w, 512)
  48. }
  49. for len(msg) > 0 && err == nil {
  50. msg, scratch, err = writeNext(dst, msg, scratch, 0)
  51. }
  52. if !cast && err == nil {
  53. err = dst.(*bufio.Writer).Flush()
  54. }
  55. return msg, err
  56. }
  57. func writeNext(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
  58. if len(msg) < 1 {
  59. return msg, scratch, ErrShortBytes
  60. }
  61. t := getType(msg[0])
  62. if t == InvalidType {
  63. return msg, scratch, InvalidPrefixError(msg[0])
  64. }
  65. if t == ExtensionType {
  66. et, err := peekExtension(msg)
  67. if err != nil {
  68. return nil, scratch, err
  69. }
  70. if et == TimeExtension || et == MsgTimeExtension {
  71. t = TimeType
  72. }
  73. }
  74. return unfuns[t](w, msg, scratch, depth)
  75. }
  76. func rwArrayBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
  77. if depth >= recursionLimit {
  78. return msg, scratch, ErrRecursion
  79. }
  80. sz, msg, err := ReadArrayHeaderBytes(msg)
  81. if err != nil {
  82. return msg, scratch, err
  83. }
  84. err = w.WriteByte('[')
  85. if err != nil {
  86. return msg, scratch, err
  87. }
  88. for i := range sz {
  89. if i != 0 {
  90. err = w.WriteByte(',')
  91. if err != nil {
  92. return msg, scratch, err
  93. }
  94. }
  95. msg, scratch, err = writeNext(w, msg, scratch, depth+1)
  96. if err != nil {
  97. return msg, scratch, err
  98. }
  99. }
  100. err = w.WriteByte(']')
  101. return msg, scratch, err
  102. }
  103. func rwMapBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
  104. if depth >= recursionLimit {
  105. return msg, scratch, ErrRecursion
  106. }
  107. sz, msg, err := ReadMapHeaderBytes(msg)
  108. if err != nil {
  109. return msg, scratch, err
  110. }
  111. err = w.WriteByte('{')
  112. if err != nil {
  113. return msg, scratch, err
  114. }
  115. for i := range sz {
  116. if i != 0 {
  117. err = w.WriteByte(',')
  118. if err != nil {
  119. return msg, scratch, err
  120. }
  121. }
  122. msg, scratch, err = rwMapKeyBytes(w, msg, scratch, depth)
  123. if err != nil {
  124. return msg, scratch, err
  125. }
  126. err = w.WriteByte(':')
  127. if err != nil {
  128. return msg, scratch, err
  129. }
  130. msg, scratch, err = writeNext(w, msg, scratch, depth+1)
  131. if err != nil {
  132. return msg, scratch, err
  133. }
  134. }
  135. err = w.WriteByte('}')
  136. return msg, scratch, err
  137. }
  138. func rwMapKeyBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
  139. if len(msg) < 1 {
  140. return msg, scratch, ErrShortBytes
  141. }
  142. switch getType(msg[0]) {
  143. case IntType:
  144. i, msg, err := ReadInt64Bytes(msg)
  145. if err != nil {
  146. return msg, scratch, err
  147. }
  148. scratch = strconv.AppendInt(scratch[:0], i, 10)
  149. _, err = rwquoted(w, scratch)
  150. return msg, scratch, err
  151. case UintType:
  152. u, msg, err := ReadUint64Bytes(msg)
  153. if err != nil {
  154. return msg, scratch, err
  155. }
  156. scratch = strconv.AppendUint(scratch[:0], u, 10)
  157. _, err = rwquoted(w, scratch)
  158. return msg, scratch, err
  159. }
  160. msg, scratch, err := rwStringBytes(w, msg, scratch, depth)
  161. if err != nil {
  162. if tperr, ok := err.(TypeError); ok && tperr.Encoded == BinType {
  163. return rwBytesBytes(w, msg, scratch, depth)
  164. }
  165. }
  166. return msg, scratch, err
  167. }
  168. func rwStringBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
  169. str, msg, err := ReadStringZC(msg)
  170. if err != nil {
  171. return msg, scratch, err
  172. }
  173. _, err = rwquoted(w, str)
  174. return msg, scratch, err
  175. }
  176. func rwBytesBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
  177. bts, msg, err := ReadBytesZC(msg)
  178. if err != nil {
  179. return msg, scratch, err
  180. }
  181. l := base64.StdEncoding.EncodedLen(len(bts))
  182. if cap(scratch) >= l {
  183. scratch = scratch[0:l]
  184. } else {
  185. scratch = make([]byte, l)
  186. }
  187. base64.StdEncoding.Encode(scratch, bts)
  188. err = w.WriteByte('"')
  189. if err != nil {
  190. return msg, scratch, err
  191. }
  192. _, err = w.Write(scratch)
  193. if err != nil {
  194. return msg, scratch, err
  195. }
  196. err = w.WriteByte('"')
  197. return msg, scratch, err
  198. }
  199. func rwNullBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
  200. msg, err := ReadNilBytes(msg)
  201. if err != nil {
  202. return msg, scratch, err
  203. }
  204. _, err = w.Write(null)
  205. return msg, scratch, err
  206. }
  207. func rwBoolBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
  208. b, msg, err := ReadBoolBytes(msg)
  209. if err != nil {
  210. return msg, scratch, err
  211. }
  212. if b {
  213. _, err = w.WriteString("true")
  214. return msg, scratch, err
  215. }
  216. _, err = w.WriteString("false")
  217. return msg, scratch, err
  218. }
  219. func rwIntBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
  220. i, msg, err := ReadInt64Bytes(msg)
  221. if err != nil {
  222. return msg, scratch, err
  223. }
  224. scratch = strconv.AppendInt(scratch[0:0], i, 10)
  225. _, err = w.Write(scratch)
  226. return msg, scratch, err
  227. }
  228. func rwUintBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
  229. u, msg, err := ReadUint64Bytes(msg)
  230. if err != nil {
  231. return msg, scratch, err
  232. }
  233. scratch = strconv.AppendUint(scratch[0:0], u, 10)
  234. _, err = w.Write(scratch)
  235. return msg, scratch, err
  236. }
  237. func rwFloat32Bytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
  238. var f float32
  239. var err error
  240. f, msg, err = ReadFloat32Bytes(msg)
  241. if err != nil {
  242. return msg, scratch, err
  243. }
  244. scratch = strconv.AppendFloat(scratch[:0], float64(f), 'f', -1, 32)
  245. _, err = w.Write(scratch)
  246. return msg, scratch, err
  247. }
  248. func rwFloat64Bytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
  249. var f float64
  250. var err error
  251. f, msg, err = ReadFloat64Bytes(msg)
  252. if err != nil {
  253. return msg, scratch, err
  254. }
  255. scratch = strconv.AppendFloat(scratch[:0], f, 'f', -1, 64)
  256. _, err = w.Write(scratch)
  257. return msg, scratch, err
  258. }
  259. func rwTimeBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
  260. var t time.Time
  261. var err error
  262. t, msg, err = ReadTimeBytes(msg)
  263. if err != nil {
  264. return msg, scratch, err
  265. }
  266. bts, err := t.MarshalJSON()
  267. if err != nil {
  268. return msg, scratch, err
  269. }
  270. _, err = w.Write(bts)
  271. return msg, scratch, err
  272. }
  273. func rwExtensionBytes(w jsWriter, msg []byte, scratch []byte, depth int) ([]byte, []byte, error) {
  274. var err error
  275. var et int8
  276. et, err = peekExtension(msg)
  277. if err != nil {
  278. return msg, scratch, err
  279. }
  280. // if it's time.Time
  281. if et == TimeExtension || et == MsgTimeExtension {
  282. var tm time.Time
  283. tm, msg, err = ReadTimeBytes(msg)
  284. if err != nil {
  285. return msg, scratch, err
  286. }
  287. bts, err := tm.MarshalJSON()
  288. if err != nil {
  289. return msg, scratch, err
  290. }
  291. _, err = w.Write(bts)
  292. return msg, scratch, err
  293. }
  294. // if the extension is registered,
  295. // use its canonical JSON form
  296. if f, ok := extensionReg[et]; ok {
  297. e := f()
  298. msg, err = ReadExtensionBytes(msg, e)
  299. if err != nil {
  300. return msg, scratch, err
  301. }
  302. bts, err := json.Marshal(e)
  303. if err != nil {
  304. return msg, scratch, err
  305. }
  306. _, err = w.Write(bts)
  307. return msg, scratch, err
  308. }
  309. // otherwise, write `{"type": <num>, "data": "<base64data>"}`
  310. r := RawExtension{}
  311. r.Type = et
  312. msg, err = ReadExtensionBytes(msg, &r)
  313. if err != nil {
  314. return msg, scratch, err
  315. }
  316. scratch, err = writeExt(w, r, scratch)
  317. return msg, scratch, err
  318. }
  319. func writeExt(w jsWriter, r RawExtension, scratch []byte) ([]byte, error) {
  320. _, err := w.WriteString(`{"type":`)
  321. if err != nil {
  322. return scratch, err
  323. }
  324. scratch = strconv.AppendInt(scratch[0:0], int64(r.Type), 10)
  325. _, err = w.Write(scratch)
  326. if err != nil {
  327. return scratch, err
  328. }
  329. _, err = w.WriteString(`,"data":"`)
  330. if err != nil {
  331. return scratch, err
  332. }
  333. l := base64.StdEncoding.EncodedLen(len(r.Data))
  334. if cap(scratch) >= l {
  335. scratch = scratch[0:l]
  336. } else {
  337. scratch = make([]byte, l)
  338. }
  339. base64.StdEncoding.Encode(scratch, r.Data)
  340. _, err = w.Write(scratch)
  341. if err != nil {
  342. return scratch, err
  343. }
  344. _, err = w.WriteString(`"}`)
  345. return scratch, err
  346. }