json_protocol.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package thrift
  20. import (
  21. "encoding/base64"
  22. "fmt"
  23. )
  24. const (
  25. THRIFT_JSON_PROTOCOL_VERSION = 1
  26. )
  27. // for references to _ParseContext see tsimplejson_protocol.go
  28. // JSON protocol implementation for thrift.
  29. //
  30. // This protocol produces/consumes a simple output format
  31. // suitable for parsing by scripting languages. It should not be
  32. // confused with the full-featured TJSONProtocol.
  33. //
  34. type TJSONProtocol struct {
  35. *TSimpleJSONProtocol
  36. }
  37. // Constructor
  38. func NewTJSONProtocol(t TTransport) *TJSONProtocol {
  39. v := &TJSONProtocol{TSimpleJSONProtocol: NewTSimpleJSONProtocol(t)}
  40. v.parseContextStack = append(v.parseContextStack, int(_CONTEXT_IN_TOPLEVEL))
  41. v.dumpContext = append(v.dumpContext, int(_CONTEXT_IN_TOPLEVEL))
  42. return v
  43. }
  44. // Factory
  45. type TJSONProtocolFactory struct{}
  46. func (p *TJSONProtocolFactory) GetProtocol(trans TTransport) TProtocol {
  47. return NewTJSONProtocol(trans)
  48. }
  49. func NewTJSONProtocolFactory() *TJSONProtocolFactory {
  50. return &TJSONProtocolFactory{}
  51. }
  52. func (p *TJSONProtocol) WriteMessageBegin(name string, typeId TMessageType, seqId int32) error {
  53. if e := p.OutputListBegin(); e != nil {
  54. return e
  55. }
  56. if e := p.WriteI32(THRIFT_JSON_PROTOCOL_VERSION); e != nil {
  57. return e
  58. }
  59. if e := p.WriteString(name); e != nil {
  60. return e
  61. }
  62. if e := p.WriteByte(byte(typeId)); e != nil {
  63. return e
  64. }
  65. if e := p.WriteI32(seqId); e != nil {
  66. return e
  67. }
  68. return nil
  69. }
  70. func (p *TJSONProtocol) WriteMessageEnd() error {
  71. return p.OutputListEnd()
  72. }
  73. func (p *TJSONProtocol) WriteStructBegin(name string) error {
  74. if e := p.OutputObjectBegin(); e != nil {
  75. return e
  76. }
  77. return nil
  78. }
  79. func (p *TJSONProtocol) WriteStructEnd() error {
  80. return p.OutputObjectEnd()
  81. }
  82. func (p *TJSONProtocol) WriteFieldBegin(name string, typeId TType, id int16) error {
  83. if e := p.WriteI16(id); e != nil {
  84. return e
  85. }
  86. if e := p.OutputObjectBegin(); e != nil {
  87. return e
  88. }
  89. s, e1 := p.TypeIdToString(typeId)
  90. if e1 != nil {
  91. return e1
  92. }
  93. if e := p.WriteString(s); e != nil {
  94. return e
  95. }
  96. return nil
  97. }
  98. func (p *TJSONProtocol) WriteFieldEnd() error {
  99. return p.OutputObjectEnd()
  100. }
  101. func (p *TJSONProtocol) WriteFieldStop() error { return nil }
  102. func (p *TJSONProtocol) WriteMapBegin(keyType TType, valueType TType, size int) error {
  103. if e := p.OutputListBegin(); e != nil {
  104. return e
  105. }
  106. s, e1 := p.TypeIdToString(keyType)
  107. if e1 != nil {
  108. return e1
  109. }
  110. if e := p.WriteString(s); e != nil {
  111. return e
  112. }
  113. s, e1 = p.TypeIdToString(valueType)
  114. if e1 != nil {
  115. return e1
  116. }
  117. if e := p.WriteString(s); e != nil {
  118. return e
  119. }
  120. return p.WriteI64(int64(size))
  121. }
  122. func (p *TJSONProtocol) WriteMapEnd() error {
  123. return p.OutputListEnd()
  124. }
  125. func (p *TJSONProtocol) WriteListBegin(elemType TType, size int) error {
  126. return p.OutputElemListBegin(elemType, size)
  127. }
  128. func (p *TJSONProtocol) WriteListEnd() error {
  129. return p.OutputListEnd()
  130. }
  131. func (p *TJSONProtocol) WriteSetBegin(elemType TType, size int) error {
  132. return p.OutputElemListBegin(elemType, size)
  133. }
  134. func (p *TJSONProtocol) WriteSetEnd() error {
  135. return p.OutputListEnd()
  136. }
  137. func (p *TJSONProtocol) WriteBool(b bool) error {
  138. if b {
  139. return p.WriteI32(1)
  140. }
  141. return p.WriteI32(0)
  142. }
  143. func (p *TJSONProtocol) WriteByte(b byte) error {
  144. return p.WriteI32(int32(b))
  145. }
  146. func (p *TJSONProtocol) WriteI16(v int16) error {
  147. return p.WriteI32(int32(v))
  148. }
  149. func (p *TJSONProtocol) WriteI32(v int32) error {
  150. return p.OutputI64(int64(v))
  151. }
  152. func (p *TJSONProtocol) WriteI64(v int64) error {
  153. return p.OutputI64(int64(v))
  154. }
  155. func (p *TJSONProtocol) WriteDouble(v float64) error {
  156. return p.OutputF64(v)
  157. }
  158. func (p *TJSONProtocol) WriteString(v string) error {
  159. return p.OutputString(v)
  160. }
  161. func (p *TJSONProtocol) WriteBinary(v []byte) error {
  162. // JSON library only takes in a string,
  163. // not an arbitrary byte array, to ensure bytes are transmitted
  164. // efficiently we must convert this into a valid JSON string
  165. // therefore we use base64 encoding to avoid excessive escaping/quoting
  166. if e := p.OutputPreValue(); e != nil {
  167. return e
  168. }
  169. p.writer.Write(JSON_QUOTE_BYTES)
  170. writer := base64.NewEncoder(base64.StdEncoding, p.writer)
  171. if _, e := writer.Write(v); e != nil {
  172. return NewTProtocolException(e)
  173. }
  174. writer.Close()
  175. p.writer.Write(JSON_QUOTE_BYTES)
  176. return p.OutputPostValue()
  177. }
  178. // Reading methods.
  179. func (p *TJSONProtocol) ReadMessageBegin() (name string, typeId TMessageType, seqId int32, err error) {
  180. if isNull, err := p.ParseListBegin(); isNull || err != nil {
  181. return name, typeId, seqId, err
  182. }
  183. version, err := p.ReadI32()
  184. if err != nil {
  185. return name, typeId, seqId, err
  186. }
  187. if version != THRIFT_JSON_PROTOCOL_VERSION {
  188. e := fmt.Errorf("Unknown Protocol version %d, expected version %d", version, THRIFT_JSON_PROTOCOL_VERSION)
  189. return name, typeId, seqId, NewTProtocolExceptionWithType(INVALID_DATA, e)
  190. }
  191. if name, err = p.ReadString(); err != nil {
  192. return name, typeId, seqId, err
  193. }
  194. bTypeId, err := p.ReadByte()
  195. typeId = TMessageType(bTypeId)
  196. if err != nil {
  197. return name, typeId, seqId, err
  198. }
  199. if seqId, err = p.ReadI32(); err != nil {
  200. return name, typeId, seqId, err
  201. }
  202. return name, typeId, seqId, nil
  203. }
  204. func (p *TJSONProtocol) ReadMessageEnd() error {
  205. err := p.ParseListEnd()
  206. return err
  207. }
  208. func (p *TJSONProtocol) ReadStructBegin() (name string, err error) {
  209. _, err = p.ParseObjectStart()
  210. return "", err
  211. }
  212. func (p *TJSONProtocol) ReadStructEnd() error {
  213. return p.ParseObjectEnd()
  214. }
  215. func (p *TJSONProtocol) ReadFieldBegin() (string, TType, int16, error) {
  216. if p.reader.Buffered() < 1 {
  217. return "", STOP, -1, nil
  218. }
  219. b, _ := p.reader.Peek(1)
  220. if len(b) < 1 || b[0] == JSON_RBRACE[0] || b[0] == JSON_RBRACKET[0] {
  221. return "", STOP, -1, nil
  222. }
  223. fieldId, err := p.ReadI16()
  224. if err != nil {
  225. return "", STOP, fieldId, err
  226. }
  227. if _, err = p.ParseObjectStart(); err != nil {
  228. return "", STOP, fieldId, err
  229. }
  230. sType, err := p.ReadString()
  231. if err != nil {
  232. return "", STOP, fieldId, err
  233. }
  234. fType, err := p.StringToTypeId(sType)
  235. return "", fType, fieldId, err
  236. }
  237. func (p *TJSONProtocol) ReadFieldEnd() error {
  238. return p.ParseObjectEnd()
  239. }
  240. func (p *TJSONProtocol) ReadMapBegin() (keyType TType, valueType TType, size int, e error) {
  241. if isNull, e := p.ParseListBegin(); isNull || e != nil {
  242. return VOID, VOID, 0, e
  243. }
  244. // read keyType
  245. sKeyType, e := p.ReadString()
  246. if e != nil {
  247. return keyType, valueType, size, e
  248. }
  249. keyType, e = p.StringToTypeId(sKeyType)
  250. if e != nil {
  251. return keyType, valueType, size, e
  252. }
  253. // read valueType
  254. sValueType, e := p.ReadString()
  255. if e != nil {
  256. return keyType, valueType, size, e
  257. }
  258. valueType, e = p.StringToTypeId(sValueType)
  259. if e != nil {
  260. return keyType, valueType, size, e
  261. }
  262. // read size
  263. iSize, err := p.ReadI64()
  264. size = int(iSize)
  265. return keyType, valueType, size, err
  266. }
  267. func (p *TJSONProtocol) ReadMapEnd() error {
  268. return p.ParseListEnd()
  269. }
  270. func (p *TJSONProtocol) ReadListBegin() (elemType TType, size int, e error) {
  271. return p.ParseElemListBegin()
  272. }
  273. func (p *TJSONProtocol) ReadListEnd() error {
  274. return p.ParseListEnd()
  275. }
  276. func (p *TJSONProtocol) ReadSetBegin() (elemType TType, size int, e error) {
  277. return p.ParseElemListBegin()
  278. }
  279. func (p *TJSONProtocol) ReadSetEnd() error {
  280. return p.ParseListEnd()
  281. }
  282. func (p *TJSONProtocol) ReadBool() (bool, error) {
  283. value, err := p.ReadI32();
  284. return (value != 0), err
  285. }
  286. func (p *TJSONProtocol) ReadByte() (byte, error) {
  287. v, err := p.ReadI64()
  288. return byte(v), err
  289. }
  290. func (p *TJSONProtocol) ReadI16() (int16, error) {
  291. v, err := p.ReadI64()
  292. return int16(v), err
  293. }
  294. func (p *TJSONProtocol) ReadI32() (int32, error) {
  295. v, err := p.ReadI64()
  296. return int32(v), err
  297. }
  298. func (p *TJSONProtocol) ReadI64() (int64, error) {
  299. v, _, err := p.ParseI64()
  300. return v, err
  301. }
  302. func (p *TJSONProtocol) ReadDouble() (float64, error) {
  303. v, _, err := p.ParseF64()
  304. return v, err
  305. }
  306. func (p *TJSONProtocol) ReadString() (string, error) {
  307. var v string
  308. if err := p.ParsePreValue(); err != nil {
  309. return v, err
  310. }
  311. b, _ := p.reader.Peek(len(JSON_NULL))
  312. if len(b) > 0 && b[0] == JSON_QUOTE {
  313. p.reader.ReadByte()
  314. value, err := p.ParseStringBody()
  315. v = value
  316. if err != nil {
  317. return v, err
  318. }
  319. } else if len(b) >= len(JSON_NULL) && string(b[0:len(JSON_NULL)]) == string(JSON_NULL) {
  320. _, err := p.reader.Read(b[0:len(JSON_NULL)])
  321. if err != nil {
  322. return v, NewTProtocolException(err)
  323. }
  324. } else {
  325. e := fmt.Errorf("Expected a JSON string, found %s", string(b))
  326. return v, NewTProtocolExceptionWithType(INVALID_DATA, e)
  327. }
  328. return v, p.ParsePostValue()
  329. }
  330. func (p *TJSONProtocol) ReadBinary() ([]byte, error) {
  331. var v []byte
  332. if err := p.ParsePreValue(); err != nil {
  333. return nil, err
  334. }
  335. b, _ := p.reader.Peek(len(JSON_NULL))
  336. if len(b) > 0 && b[0] == JSON_QUOTE {
  337. p.reader.ReadByte()
  338. value, err := p.ParseBase64EncodedBody()
  339. v = value
  340. if err != nil {
  341. return v, err
  342. }
  343. } else if len(b) >= len(JSON_NULL) && string(b[0:len(JSON_NULL)]) == string(JSON_NULL) {
  344. _, err := p.reader.Read(b[0:len(JSON_NULL)])
  345. if err != nil {
  346. return v, NewTProtocolException(err)
  347. }
  348. } else {
  349. e := fmt.Errorf("Expected a JSON string, found %s", string(b))
  350. return v, NewTProtocolExceptionWithType(INVALID_DATA, e)
  351. }
  352. return v, p.ParsePostValue()
  353. }
  354. func (p *TJSONProtocol) Flush() (err error) {
  355. err = p.writer.Flush()
  356. if err == nil {
  357. err = p.trans.Flush()
  358. }
  359. return NewTProtocolException(err)
  360. }
  361. func (p *TJSONProtocol) Skip(fieldType TType) (err error) {
  362. return SkipDefaultDepth(p, fieldType)
  363. }
  364. func (p *TJSONProtocol) Transport() TTransport {
  365. return p.trans
  366. }
  367. func (p *TJSONProtocol) OutputElemListBegin(elemType TType, size int) error {
  368. if e := p.OutputListBegin(); e != nil {
  369. return e
  370. }
  371. s, e1 := p.TypeIdToString(elemType)
  372. if e1 != nil {
  373. return e1
  374. }
  375. if e := p.WriteString(s); e != nil {
  376. return e
  377. }
  378. if e := p.WriteI64(int64(size)); e != nil {
  379. return e
  380. }
  381. return nil
  382. }
  383. func (p *TJSONProtocol) ParseElemListBegin() (elemType TType, size int, e error) {
  384. if isNull, e := p.ParseListBegin(); isNull || e != nil {
  385. return VOID, 0, e
  386. }
  387. sElemType, err := p.ReadString()
  388. if err != nil {
  389. return VOID, size, err
  390. }
  391. elemType, err = p.StringToTypeId(sElemType)
  392. if err != nil {
  393. return elemType, size, err
  394. }
  395. nSize, err2 := p.ReadI64()
  396. size = int(nSize)
  397. return elemType, size, err2
  398. }
  399. func (p *TJSONProtocol) readElemListBegin() (elemType TType, size int, e error) {
  400. if isNull, e := p.ParseListBegin(); isNull || e != nil {
  401. return VOID, 0, e
  402. }
  403. sElemType, err := p.ReadString()
  404. if err != nil {
  405. return VOID, size, err
  406. }
  407. elemType, err = p.StringToTypeId(sElemType)
  408. if err != nil {
  409. return elemType, size, err
  410. }
  411. nSize, err2 := p.ReadI64()
  412. size = int(nSize)
  413. return elemType, size, err2
  414. }
  415. func (p *TJSONProtocol) writeElemListBegin(elemType TType, size int) error {
  416. if e := p.OutputListBegin(); e != nil {
  417. return e
  418. }
  419. s, e1 := p.TypeIdToString(elemType)
  420. if e1 != nil {
  421. return e1
  422. }
  423. if e := p.OutputString(s); e != nil {
  424. return e
  425. }
  426. if e := p.OutputI64(int64(size)); e != nil {
  427. return e
  428. }
  429. return nil
  430. }
  431. func (p *TJSONProtocol) TypeIdToString(fieldType TType) (string, error) {
  432. switch byte(fieldType) {
  433. case BOOL:
  434. return "tf", nil
  435. case BYTE:
  436. return "i8", nil
  437. case I16:
  438. return "i16", nil
  439. case I32:
  440. return "i32", nil
  441. case I64:
  442. return "i64", nil
  443. case DOUBLE:
  444. return "dbl", nil
  445. case STRING:
  446. return "str", nil
  447. case STRUCT:
  448. return "rec", nil
  449. case MAP:
  450. return "map", nil
  451. case SET:
  452. return "set", nil
  453. case LIST:
  454. return "lst", nil
  455. }
  456. e := fmt.Errorf("Unknown fieldType: %d", int(fieldType))
  457. return "", NewTProtocolExceptionWithType(INVALID_DATA, e)
  458. }
  459. func (p *TJSONProtocol) StringToTypeId(fieldType string) (TType, error) {
  460. switch fieldType {
  461. case "tf":
  462. return TType(BOOL), nil
  463. case "i8":
  464. return TType(BYTE), nil
  465. case "i16":
  466. return TType(I16), nil
  467. case "i32":
  468. return TType(I32), nil
  469. case "i64":
  470. return TType(I64), nil
  471. case "dbl":
  472. return TType(DOUBLE), nil
  473. case "str":
  474. return TType(STRING), nil
  475. case "rec":
  476. return TType(STRUCT), nil
  477. case "map":
  478. return TType(MAP), nil
  479. case "set":
  480. return TType(SET), nil
  481. case "lst":
  482. return TType(LIST), nil
  483. }
  484. e := fmt.Errorf("Unknown type identifier: %s", fieldType)
  485. return TType(STOP), NewTProtocolExceptionWithType(INVALID_DATA, e)
  486. }