json_protocol.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. p.resetContextStack() // THRIFT-3735
  54. if e := p.OutputListBegin(); e != nil {
  55. return e
  56. }
  57. if e := p.WriteI32(THRIFT_JSON_PROTOCOL_VERSION); e != nil {
  58. return e
  59. }
  60. if e := p.WriteString(name); e != nil {
  61. return e
  62. }
  63. if e := p.WriteByte(int8(typeId)); e != nil {
  64. return e
  65. }
  66. if e := p.WriteI32(seqId); e != nil {
  67. return e
  68. }
  69. return nil
  70. }
  71. func (p *TJSONProtocol) WriteMessageEnd() error {
  72. return p.OutputListEnd()
  73. }
  74. func (p *TJSONProtocol) WriteStructBegin(name string) error {
  75. if e := p.OutputObjectBegin(); e != nil {
  76. return e
  77. }
  78. return nil
  79. }
  80. func (p *TJSONProtocol) WriteStructEnd() error {
  81. return p.OutputObjectEnd()
  82. }
  83. func (p *TJSONProtocol) WriteFieldBegin(name string, typeId TType, id int16) error {
  84. if e := p.WriteI16(id); e != nil {
  85. return e
  86. }
  87. if e := p.OutputObjectBegin(); e != nil {
  88. return e
  89. }
  90. s, e1 := p.TypeIdToString(typeId)
  91. if e1 != nil {
  92. return e1
  93. }
  94. if e := p.WriteString(s); e != nil {
  95. return e
  96. }
  97. return nil
  98. }
  99. func (p *TJSONProtocol) WriteFieldEnd() error {
  100. return p.OutputObjectEnd()
  101. }
  102. func (p *TJSONProtocol) WriteFieldStop() error { return nil }
  103. func (p *TJSONProtocol) WriteMapBegin(keyType TType, valueType TType, size int) error {
  104. if e := p.OutputListBegin(); e != nil {
  105. return e
  106. }
  107. s, e1 := p.TypeIdToString(keyType)
  108. if e1 != nil {
  109. return e1
  110. }
  111. if e := p.WriteString(s); e != nil {
  112. return e
  113. }
  114. s, e1 = p.TypeIdToString(valueType)
  115. if e1 != nil {
  116. return e1
  117. }
  118. if e := p.WriteString(s); e != nil {
  119. return e
  120. }
  121. if e := p.WriteI64(int64(size)); e != nil {
  122. return e
  123. }
  124. return p.OutputObjectBegin()
  125. }
  126. func (p *TJSONProtocol) WriteMapEnd() error {
  127. if e := p.OutputObjectEnd(); e != nil {
  128. return e
  129. }
  130. return p.OutputListEnd()
  131. }
  132. func (p *TJSONProtocol) WriteListBegin(elemType TType, size int) error {
  133. return p.OutputElemListBegin(elemType, size)
  134. }
  135. func (p *TJSONProtocol) WriteListEnd() error {
  136. return p.OutputListEnd()
  137. }
  138. func (p *TJSONProtocol) WriteSetBegin(elemType TType, size int) error {
  139. return p.OutputElemListBegin(elemType, size)
  140. }
  141. func (p *TJSONProtocol) WriteSetEnd() error {
  142. return p.OutputListEnd()
  143. }
  144. func (p *TJSONProtocol) WriteBool(b bool) error {
  145. if b {
  146. return p.WriteI32(1)
  147. }
  148. return p.WriteI32(0)
  149. }
  150. func (p *TJSONProtocol) WriteByte(b int8) error {
  151. return p.WriteI32(int32(b))
  152. }
  153. func (p *TJSONProtocol) WriteI16(v int16) error {
  154. return p.WriteI32(int32(v))
  155. }
  156. func (p *TJSONProtocol) WriteI32(v int32) error {
  157. return p.OutputI64(int64(v))
  158. }
  159. func (p *TJSONProtocol) WriteI64(v int64) error {
  160. return p.OutputI64(int64(v))
  161. }
  162. func (p *TJSONProtocol) WriteDouble(v float64) error {
  163. return p.OutputF64(v)
  164. }
  165. func (p *TJSONProtocol) WriteString(v string) error {
  166. return p.OutputString(v)
  167. }
  168. func (p *TJSONProtocol) WriteBinary(v []byte) error {
  169. // JSON library only takes in a string,
  170. // not an arbitrary byte array, to ensure bytes are transmitted
  171. // efficiently we must convert this into a valid JSON string
  172. // therefore we use base64 encoding to avoid excessive escaping/quoting
  173. if e := p.OutputPreValue(); e != nil {
  174. return e
  175. }
  176. if _, e := p.write(JSON_QUOTE_BYTES); e != nil {
  177. return NewTProtocolException(e)
  178. }
  179. writer := base64.NewEncoder(base64.StdEncoding, p.writer)
  180. if _, e := writer.Write(v); e != nil {
  181. p.writer.Reset(p.trans) // THRIFT-3735
  182. return NewTProtocolException(e)
  183. }
  184. if e := writer.Close(); e != nil {
  185. return NewTProtocolException(e)
  186. }
  187. if _, e := p.write(JSON_QUOTE_BYTES); e != nil {
  188. return NewTProtocolException(e)
  189. }
  190. return p.OutputPostValue()
  191. }
  192. // Reading methods.
  193. func (p *TJSONProtocol) ReadMessageBegin() (name string, typeId TMessageType, seqId int32, err error) {
  194. p.resetContextStack() // THRIFT-3735
  195. if isNull, err := p.ParseListBegin(); isNull || err != nil {
  196. return name, typeId, seqId, err
  197. }
  198. version, err := p.ReadI32()
  199. if err != nil {
  200. return name, typeId, seqId, err
  201. }
  202. if version != THRIFT_JSON_PROTOCOL_VERSION {
  203. e := fmt.Errorf("Unknown Protocol version %d, expected version %d", version, THRIFT_JSON_PROTOCOL_VERSION)
  204. return name, typeId, seqId, NewTProtocolExceptionWithType(INVALID_DATA, e)
  205. }
  206. if name, err = p.ReadString(); err != nil {
  207. return name, typeId, seqId, err
  208. }
  209. bTypeId, err := p.ReadByte()
  210. typeId = TMessageType(bTypeId)
  211. if err != nil {
  212. return name, typeId, seqId, err
  213. }
  214. if seqId, err = p.ReadI32(); err != nil {
  215. return name, typeId, seqId, err
  216. }
  217. return name, typeId, seqId, nil
  218. }
  219. func (p *TJSONProtocol) ReadMessageEnd() error {
  220. err := p.ParseListEnd()
  221. return err
  222. }
  223. func (p *TJSONProtocol) ReadStructBegin() (name string, err error) {
  224. _, err = p.ParseObjectStart()
  225. return "", err
  226. }
  227. func (p *TJSONProtocol) ReadStructEnd() error {
  228. return p.ParseObjectEnd()
  229. }
  230. func (p *TJSONProtocol) ReadFieldBegin() (string, TType, int16, error) {
  231. b, _ := p.reader.Peek(1)
  232. if len(b) < 1 || b[0] == JSON_RBRACE[0] || b[0] == JSON_RBRACKET[0] {
  233. return "", STOP, -1, nil
  234. }
  235. fieldId, err := p.ReadI16()
  236. if err != nil {
  237. return "", STOP, fieldId, err
  238. }
  239. if _, err = p.ParseObjectStart(); err != nil {
  240. return "", STOP, fieldId, err
  241. }
  242. sType, err := p.ReadString()
  243. if err != nil {
  244. return "", STOP, fieldId, err
  245. }
  246. fType, err := p.StringToTypeId(sType)
  247. return "", fType, fieldId, err
  248. }
  249. func (p *TJSONProtocol) ReadFieldEnd() error {
  250. return p.ParseObjectEnd()
  251. }
  252. func (p *TJSONProtocol) ReadMapBegin() (keyType TType, valueType TType, size int, e error) {
  253. if isNull, e := p.ParseListBegin(); isNull || e != nil {
  254. return VOID, VOID, 0, e
  255. }
  256. // read keyType
  257. sKeyType, e := p.ReadString()
  258. if e != nil {
  259. return keyType, valueType, size, e
  260. }
  261. keyType, e = p.StringToTypeId(sKeyType)
  262. if e != nil {
  263. return keyType, valueType, size, e
  264. }
  265. // read valueType
  266. sValueType, e := p.ReadString()
  267. if e != nil {
  268. return keyType, valueType, size, e
  269. }
  270. valueType, e = p.StringToTypeId(sValueType)
  271. if e != nil {
  272. return keyType, valueType, size, e
  273. }
  274. // read size
  275. iSize, e := p.ReadI64()
  276. if e != nil {
  277. return keyType, valueType, size, e
  278. }
  279. size = int(iSize)
  280. _, e = p.ParseObjectStart()
  281. return keyType, valueType, size, e
  282. }
  283. func (p *TJSONProtocol) ReadMapEnd() error {
  284. e := p.ParseObjectEnd()
  285. if e != nil {
  286. return e
  287. }
  288. return p.ParseListEnd()
  289. }
  290. func (p *TJSONProtocol) ReadListBegin() (elemType TType, size int, e error) {
  291. return p.ParseElemListBegin()
  292. }
  293. func (p *TJSONProtocol) ReadListEnd() error {
  294. return p.ParseListEnd()
  295. }
  296. func (p *TJSONProtocol) ReadSetBegin() (elemType TType, size int, e error) {
  297. return p.ParseElemListBegin()
  298. }
  299. func (p *TJSONProtocol) ReadSetEnd() error {
  300. return p.ParseListEnd()
  301. }
  302. func (p *TJSONProtocol) ReadBool() (bool, error) {
  303. value, err := p.ReadI32()
  304. return (value != 0), err
  305. }
  306. func (p *TJSONProtocol) ReadByte() (int8, error) {
  307. v, err := p.ReadI64()
  308. return int8(v), err
  309. }
  310. func (p *TJSONProtocol) ReadI16() (int16, error) {
  311. v, err := p.ReadI64()
  312. return int16(v), err
  313. }
  314. func (p *TJSONProtocol) ReadI32() (int32, error) {
  315. v, err := p.ReadI64()
  316. return int32(v), err
  317. }
  318. func (p *TJSONProtocol) ReadI64() (int64, error) {
  319. v, _, err := p.ParseI64()
  320. return v, err
  321. }
  322. func (p *TJSONProtocol) ReadDouble() (float64, error) {
  323. v, _, err := p.ParseF64()
  324. return v, err
  325. }
  326. func (p *TJSONProtocol) ReadString() (string, error) {
  327. var v string
  328. if err := p.ParsePreValue(); err != nil {
  329. return v, err
  330. }
  331. f, _ := p.reader.Peek(1)
  332. if len(f) > 0 && f[0] == JSON_QUOTE {
  333. p.reader.ReadByte()
  334. value, err := p.ParseStringBody()
  335. v = value
  336. if err != nil {
  337. return v, err
  338. }
  339. } else if len(f) > 0 && f[0] == JSON_NULL[0] {
  340. b := make([]byte, len(JSON_NULL))
  341. _, err := p.reader.Read(b)
  342. if err != nil {
  343. return v, NewTProtocolException(err)
  344. }
  345. if string(b) != string(JSON_NULL) {
  346. e := fmt.Errorf("Expected a JSON string, found unquoted data started with %s", string(b))
  347. return v, NewTProtocolExceptionWithType(INVALID_DATA, e)
  348. }
  349. } else {
  350. e := fmt.Errorf("Expected a JSON string, found unquoted data started with %s", string(f))
  351. return v, NewTProtocolExceptionWithType(INVALID_DATA, e)
  352. }
  353. return v, p.ParsePostValue()
  354. }
  355. func (p *TJSONProtocol) ReadBinary() ([]byte, error) {
  356. var v []byte
  357. if err := p.ParsePreValue(); err != nil {
  358. return nil, err
  359. }
  360. f, _ := p.reader.Peek(1)
  361. if len(f) > 0 && f[0] == JSON_QUOTE {
  362. p.reader.ReadByte()
  363. value, err := p.ParseBase64EncodedBody()
  364. v = value
  365. if err != nil {
  366. return v, err
  367. }
  368. } else if len(f) > 0 && f[0] == JSON_NULL[0] {
  369. b := make([]byte, len(JSON_NULL))
  370. _, err := p.reader.Read(b)
  371. if err != nil {
  372. return v, NewTProtocolException(err)
  373. }
  374. if string(b) != string(JSON_NULL) {
  375. e := fmt.Errorf("Expected a JSON string, found unquoted data started with %s", string(b))
  376. return v, NewTProtocolExceptionWithType(INVALID_DATA, e)
  377. }
  378. } else {
  379. e := fmt.Errorf("Expected a JSON string, found unquoted data started with %s", string(f))
  380. return v, NewTProtocolExceptionWithType(INVALID_DATA, e)
  381. }
  382. return v, p.ParsePostValue()
  383. }
  384. func (p *TJSONProtocol) Flush() (err error) {
  385. err = p.writer.Flush()
  386. if err == nil {
  387. err = p.trans.Flush()
  388. }
  389. return NewTProtocolException(err)
  390. }
  391. func (p *TJSONProtocol) Skip(fieldType TType) (err error) {
  392. return SkipDefaultDepth(p, fieldType)
  393. }
  394. func (p *TJSONProtocol) Transport() TTransport {
  395. return p.trans
  396. }
  397. func (p *TJSONProtocol) OutputElemListBegin(elemType TType, size int) error {
  398. if e := p.OutputListBegin(); e != nil {
  399. return e
  400. }
  401. s, e1 := p.TypeIdToString(elemType)
  402. if e1 != nil {
  403. return e1
  404. }
  405. if e := p.WriteString(s); e != nil {
  406. return e
  407. }
  408. if e := p.WriteI64(int64(size)); e != nil {
  409. return e
  410. }
  411. return nil
  412. }
  413. func (p *TJSONProtocol) ParseElemListBegin() (elemType TType, size int, e error) {
  414. if isNull, e := p.ParseListBegin(); isNull || e != nil {
  415. return VOID, 0, e
  416. }
  417. sElemType, err := p.ReadString()
  418. if err != nil {
  419. return VOID, size, err
  420. }
  421. elemType, err = p.StringToTypeId(sElemType)
  422. if err != nil {
  423. return elemType, size, err
  424. }
  425. nSize, err2 := p.ReadI64()
  426. size = int(nSize)
  427. return elemType, size, err2
  428. }
  429. func (p *TJSONProtocol) readElemListBegin() (elemType TType, size int, e error) {
  430. if isNull, e := p.ParseListBegin(); isNull || e != nil {
  431. return VOID, 0, e
  432. }
  433. sElemType, err := p.ReadString()
  434. if err != nil {
  435. return VOID, size, err
  436. }
  437. elemType, err = p.StringToTypeId(sElemType)
  438. if err != nil {
  439. return elemType, size, err
  440. }
  441. nSize, err2 := p.ReadI64()
  442. size = int(nSize)
  443. return elemType, size, err2
  444. }
  445. func (p *TJSONProtocol) writeElemListBegin(elemType TType, size int) error {
  446. if e := p.OutputListBegin(); e != nil {
  447. return e
  448. }
  449. s, e1 := p.TypeIdToString(elemType)
  450. if e1 != nil {
  451. return e1
  452. }
  453. if e := p.OutputString(s); e != nil {
  454. return e
  455. }
  456. if e := p.OutputI64(int64(size)); e != nil {
  457. return e
  458. }
  459. return nil
  460. }
  461. func (p *TJSONProtocol) TypeIdToString(fieldType TType) (string, error) {
  462. switch byte(fieldType) {
  463. case BOOL:
  464. return "tf", nil
  465. case BYTE:
  466. return "i8", nil
  467. case I16:
  468. return "i16", nil
  469. case I32:
  470. return "i32", nil
  471. case I64:
  472. return "i64", nil
  473. case DOUBLE:
  474. return "dbl", nil
  475. case STRING:
  476. return "str", nil
  477. case STRUCT:
  478. return "rec", nil
  479. case MAP:
  480. return "map", nil
  481. case SET:
  482. return "set", nil
  483. case LIST:
  484. return "lst", nil
  485. }
  486. e := fmt.Errorf("Unknown fieldType: %d", int(fieldType))
  487. return "", NewTProtocolExceptionWithType(INVALID_DATA, e)
  488. }
  489. func (p *TJSONProtocol) StringToTypeId(fieldType string) (TType, error) {
  490. switch fieldType {
  491. case "tf":
  492. return TType(BOOL), nil
  493. case "i8":
  494. return TType(BYTE), nil
  495. case "i16":
  496. return TType(I16), nil
  497. case "i32":
  498. return TType(I32), nil
  499. case "i64":
  500. return TType(I64), nil
  501. case "dbl":
  502. return TType(DOUBLE), nil
  503. case "str":
  504. return TType(STRING), nil
  505. case "rec":
  506. return TType(STRUCT), nil
  507. case "map":
  508. return TType(MAP), nil
  509. case "set":
  510. return TType(SET), nil
  511. case "lst":
  512. return TType(LIST), nil
  513. }
  514. e := fmt.Errorf("Unknown type identifier: %s", fieldType)
  515. return TType(STOP), NewTProtocolExceptionWithType(INVALID_DATA, e)
  516. }