json_bytes.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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) ([]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) ([]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)
  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) ([]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 {
  71. t = TimeType
  72. }
  73. }
  74. return unfuns[t](w, msg, scratch)
  75. }
  76. func rwArrayBytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
  77. sz, msg, err := ReadArrayHeaderBytes(msg)
  78. if err != nil {
  79. return msg, scratch, err
  80. }
  81. err = w.WriteByte('[')
  82. if err != nil {
  83. return msg, scratch, err
  84. }
  85. for i := uint32(0); i < sz; i++ {
  86. if i != 0 {
  87. err = w.WriteByte(',')
  88. if err != nil {
  89. return msg, scratch, err
  90. }
  91. }
  92. msg, scratch, err = writeNext(w, msg, scratch)
  93. if err != nil {
  94. return msg, scratch, err
  95. }
  96. }
  97. err = w.WriteByte(']')
  98. return msg, scratch, err
  99. }
  100. func rwMapBytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
  101. sz, msg, err := ReadMapHeaderBytes(msg)
  102. if err != nil {
  103. return msg, scratch, err
  104. }
  105. err = w.WriteByte('{')
  106. if err != nil {
  107. return msg, scratch, err
  108. }
  109. for i := uint32(0); i < sz; i++ {
  110. if i != 0 {
  111. err = w.WriteByte(',')
  112. if err != nil {
  113. return msg, scratch, err
  114. }
  115. }
  116. msg, scratch, err = rwMapKeyBytes(w, msg, scratch)
  117. if err != nil {
  118. return msg, scratch, err
  119. }
  120. err = w.WriteByte(':')
  121. if err != nil {
  122. return msg, scratch, err
  123. }
  124. msg, scratch, err = writeNext(w, msg, scratch)
  125. if err != nil {
  126. return msg, scratch, err
  127. }
  128. }
  129. err = w.WriteByte('}')
  130. return msg, scratch, err
  131. }
  132. func rwMapKeyBytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
  133. msg, scratch, err := rwStringBytes(w, msg, scratch)
  134. if err != nil {
  135. if tperr, ok := err.(TypeError); ok && tperr.Encoded == BinType {
  136. return rwBytesBytes(w, msg, scratch)
  137. }
  138. }
  139. return msg, scratch, err
  140. }
  141. func rwStringBytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
  142. str, msg, err := ReadStringZC(msg)
  143. if err != nil {
  144. return msg, scratch, err
  145. }
  146. _, err = rwquoted(w, str)
  147. return msg, scratch, err
  148. }
  149. func rwBytesBytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
  150. bts, msg, err := ReadBytesZC(msg)
  151. if err != nil {
  152. return msg, scratch, err
  153. }
  154. l := base64.StdEncoding.EncodedLen(len(bts))
  155. if cap(scratch) >= l {
  156. scratch = scratch[0:l]
  157. } else {
  158. scratch = make([]byte, l)
  159. }
  160. base64.StdEncoding.Encode(scratch, bts)
  161. err = w.WriteByte('"')
  162. if err != nil {
  163. return msg, scratch, err
  164. }
  165. _, err = w.Write(scratch)
  166. if err != nil {
  167. return msg, scratch, err
  168. }
  169. err = w.WriteByte('"')
  170. return msg, scratch, err
  171. }
  172. func rwNullBytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
  173. msg, err := ReadNilBytes(msg)
  174. if err != nil {
  175. return msg, scratch, err
  176. }
  177. _, err = w.Write(null)
  178. return msg, scratch, err
  179. }
  180. func rwBoolBytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
  181. b, msg, err := ReadBoolBytes(msg)
  182. if err != nil {
  183. return msg, scratch, err
  184. }
  185. if b {
  186. _, err = w.WriteString("true")
  187. return msg, scratch, err
  188. }
  189. _, err = w.WriteString("false")
  190. return msg, scratch, err
  191. }
  192. func rwIntBytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
  193. i, msg, err := ReadInt64Bytes(msg)
  194. if err != nil {
  195. return msg, scratch, err
  196. }
  197. scratch = strconv.AppendInt(scratch[0:0], i, 10)
  198. _, err = w.Write(scratch)
  199. return msg, scratch, err
  200. }
  201. func rwUintBytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
  202. u, msg, err := ReadUint64Bytes(msg)
  203. if err != nil {
  204. return msg, scratch, err
  205. }
  206. scratch = strconv.AppendUint(scratch[0:0], u, 10)
  207. _, err = w.Write(scratch)
  208. return msg, scratch, err
  209. }
  210. func rwFloatBytes(w jsWriter, msg []byte, f64 bool, scratch []byte) ([]byte, []byte, error) {
  211. var f float64
  212. var err error
  213. var sz int
  214. if f64 {
  215. sz = 64
  216. f, msg, err = ReadFloat64Bytes(msg)
  217. } else {
  218. sz = 32
  219. var v float32
  220. v, msg, err = ReadFloat32Bytes(msg)
  221. f = float64(v)
  222. }
  223. if err != nil {
  224. return msg, scratch, err
  225. }
  226. scratch = strconv.AppendFloat(scratch, f, 'f', -1, sz)
  227. _, err = w.Write(scratch)
  228. return msg, scratch, err
  229. }
  230. func rwFloat32Bytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
  231. var f float32
  232. var err error
  233. f, msg, err = ReadFloat32Bytes(msg)
  234. if err != nil {
  235. return msg, scratch, err
  236. }
  237. scratch = strconv.AppendFloat(scratch[:0], float64(f), 'f', -1, 32)
  238. _, err = w.Write(scratch)
  239. return msg, scratch, err
  240. }
  241. func rwFloat64Bytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
  242. var f float64
  243. var err error
  244. f, msg, err = ReadFloat64Bytes(msg)
  245. if err != nil {
  246. return msg, scratch, err
  247. }
  248. scratch = strconv.AppendFloat(scratch[:0], f, 'f', -1, 64)
  249. _, err = w.Write(scratch)
  250. return msg, scratch, err
  251. }
  252. func rwTimeBytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
  253. var t time.Time
  254. var err error
  255. t, msg, err = ReadTimeBytes(msg)
  256. if err != nil {
  257. return msg, scratch, err
  258. }
  259. bts, err := t.MarshalJSON()
  260. if err != nil {
  261. return msg, scratch, err
  262. }
  263. _, err = w.Write(bts)
  264. return msg, scratch, err
  265. }
  266. func rwExtensionBytes(w jsWriter, msg []byte, scratch []byte) ([]byte, []byte, error) {
  267. var err error
  268. var et int8
  269. et, err = peekExtension(msg)
  270. if err != nil {
  271. return msg, scratch, err
  272. }
  273. // if it's time.Time
  274. if et == TimeExtension {
  275. var tm time.Time
  276. tm, msg, err = ReadTimeBytes(msg)
  277. if err != nil {
  278. return msg, scratch, err
  279. }
  280. bts, err := tm.MarshalJSON()
  281. if err != nil {
  282. return msg, scratch, err
  283. }
  284. _, err = w.Write(bts)
  285. return msg, scratch, err
  286. }
  287. // if the extension is registered,
  288. // use its canonical JSON form
  289. if f, ok := extensionReg[et]; ok {
  290. e := f()
  291. msg, err = ReadExtensionBytes(msg, e)
  292. if err != nil {
  293. return msg, scratch, err
  294. }
  295. bts, err := json.Marshal(e)
  296. if err != nil {
  297. return msg, scratch, err
  298. }
  299. _, err = w.Write(bts)
  300. return msg, scratch, err
  301. }
  302. // otherwise, write `{"type": <num>, "data": "<base64data>"}`
  303. r := RawExtension{}
  304. r.Type = et
  305. msg, err = ReadExtensionBytes(msg, &r)
  306. if err != nil {
  307. return msg, scratch, err
  308. }
  309. scratch, err = writeExt(w, r, scratch)
  310. return msg, scratch, err
  311. }
  312. func writeExt(w jsWriter, r RawExtension, scratch []byte) ([]byte, error) {
  313. _, err := w.WriteString(`{"type":`)
  314. if err != nil {
  315. return scratch, err
  316. }
  317. scratch = strconv.AppendInt(scratch[0:0], int64(r.Type), 10)
  318. _, err = w.Write(scratch)
  319. if err != nil {
  320. return scratch, err
  321. }
  322. _, err = w.WriteString(`,"data":"`)
  323. if err != nil {
  324. return scratch, err
  325. }
  326. l := base64.StdEncoding.EncodedLen(len(r.Data))
  327. if cap(scratch) >= l {
  328. scratch = scratch[0:l]
  329. } else {
  330. scratch = make([]byte, l)
  331. }
  332. base64.StdEncoding.Encode(scratch, r.Data)
  333. _, err = w.Write(scratch)
  334. if err != nil {
  335. return scratch, err
  336. }
  337. _, err = w.WriteString(`"}`)
  338. return scratch, err
  339. }