simple_json_protocol.go 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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. "bufio"
  22. "bytes"
  23. "encoding/base64"
  24. "encoding/json"
  25. "fmt"
  26. "io"
  27. "math"
  28. "strconv"
  29. )
  30. type _ParseContext int
  31. const (
  32. _CONTEXT_IN_TOPLEVEL _ParseContext = 1
  33. _CONTEXT_IN_LIST_FIRST _ParseContext = 2
  34. _CONTEXT_IN_LIST _ParseContext = 3
  35. _CONTEXT_IN_OBJECT_FIRST _ParseContext = 4
  36. _CONTEXT_IN_OBJECT_NEXT_KEY _ParseContext = 5
  37. _CONTEXT_IN_OBJECT_NEXT_VALUE _ParseContext = 6
  38. )
  39. func (p _ParseContext) String() string {
  40. switch p {
  41. case _CONTEXT_IN_TOPLEVEL:
  42. return "TOPLEVEL"
  43. case _CONTEXT_IN_LIST_FIRST:
  44. return "LIST-FIRST"
  45. case _CONTEXT_IN_LIST:
  46. return "LIST"
  47. case _CONTEXT_IN_OBJECT_FIRST:
  48. return "OBJECT-FIRST"
  49. case _CONTEXT_IN_OBJECT_NEXT_KEY:
  50. return "OBJECT-NEXT-KEY"
  51. case _CONTEXT_IN_OBJECT_NEXT_VALUE:
  52. return "OBJECT-NEXT-VALUE"
  53. }
  54. return "UNKNOWN-PARSE-CONTEXT"
  55. }
  56. // JSON protocol implementation for thrift.
  57. //
  58. // This protocol produces/consumes a simple output format
  59. // suitable for parsing by scripting languages. It should not be
  60. // confused with the full-featured TJSONProtocol.
  61. //
  62. type TSimpleJSONProtocol struct {
  63. trans TTransport
  64. parseContextStack []int
  65. dumpContext []int
  66. writer *bufio.Writer
  67. reader *bufio.Reader
  68. }
  69. // Constructor
  70. func NewTSimpleJSONProtocol(t TTransport) *TSimpleJSONProtocol {
  71. v := &TSimpleJSONProtocol{trans: t,
  72. writer: bufio.NewWriter(t),
  73. reader: bufio.NewReader(t),
  74. }
  75. v.parseContextStack = append(v.parseContextStack, int(_CONTEXT_IN_TOPLEVEL))
  76. v.dumpContext = append(v.dumpContext, int(_CONTEXT_IN_TOPLEVEL))
  77. return v
  78. }
  79. // Factory
  80. type TSimpleJSONProtocolFactory struct{}
  81. func (p *TSimpleJSONProtocolFactory) GetProtocol(trans TTransport) TProtocol {
  82. return NewTSimpleJSONProtocol(trans)
  83. }
  84. func NewTSimpleJSONProtocolFactory() *TSimpleJSONProtocolFactory {
  85. return &TSimpleJSONProtocolFactory{}
  86. }
  87. var (
  88. JSON_COMMA []byte
  89. JSON_COLON []byte
  90. JSON_LBRACE []byte
  91. JSON_RBRACE []byte
  92. JSON_LBRACKET []byte
  93. JSON_RBRACKET []byte
  94. JSON_QUOTE byte
  95. JSON_QUOTE_BYTES []byte
  96. JSON_NULL []byte
  97. JSON_TRUE []byte
  98. JSON_FALSE []byte
  99. JSON_INFINITY string
  100. JSON_NEGATIVE_INFINITY string
  101. JSON_NAN string
  102. JSON_INFINITY_BYTES []byte
  103. JSON_NEGATIVE_INFINITY_BYTES []byte
  104. JSON_NAN_BYTES []byte
  105. json_nonbase_map_elem_bytes []byte
  106. )
  107. func init() {
  108. JSON_COMMA = []byte{','}
  109. JSON_COLON = []byte{':'}
  110. JSON_LBRACE = []byte{'{'}
  111. JSON_RBRACE = []byte{'}'}
  112. JSON_LBRACKET = []byte{'['}
  113. JSON_RBRACKET = []byte{']'}
  114. JSON_QUOTE = '"'
  115. JSON_QUOTE_BYTES = []byte{'"'}
  116. JSON_NULL = []byte{'n', 'u', 'l', 'l'}
  117. JSON_TRUE = []byte{'t', 'r', 'u', 'e'}
  118. JSON_FALSE = []byte{'f', 'a', 'l', 's', 'e'}
  119. JSON_INFINITY = "Infinity"
  120. JSON_NEGATIVE_INFINITY = "-Infinity"
  121. JSON_NAN = "NaN"
  122. JSON_INFINITY_BYTES = []byte{'I', 'n', 'f', 'i', 'n', 'i', 't', 'y'}
  123. JSON_NEGATIVE_INFINITY_BYTES = []byte{'-', 'I', 'n', 'f', 'i', 'n', 'i', 't', 'y'}
  124. JSON_NAN_BYTES = []byte{'N', 'a', 'N'}
  125. json_nonbase_map_elem_bytes = []byte{']', ',', '['}
  126. }
  127. func jsonQuote(s string) string {
  128. b, _ := json.Marshal(s)
  129. s1 := string(b)
  130. return s1
  131. }
  132. func jsonUnquote(s string) (string, bool) {
  133. s1 := new(string)
  134. err := json.Unmarshal([]byte(s), s1)
  135. return *s1, err == nil
  136. }
  137. func mismatch(expected, actual string) error {
  138. return fmt.Errorf("Expected '%s' but found '%s' while parsing JSON.", expected, actual)
  139. }
  140. func (p *TSimpleJSONProtocol) WriteMessageBegin(name string, typeId TMessageType, seqId int32) error {
  141. p.resetContextStack() // THRIFT-3735
  142. if e := p.OutputListBegin(); e != nil {
  143. return e
  144. }
  145. if e := p.WriteString(name); e != nil {
  146. return e
  147. }
  148. if e := p.WriteByte(int8(typeId)); e != nil {
  149. return e
  150. }
  151. if e := p.WriteI32(seqId); e != nil {
  152. return e
  153. }
  154. return nil
  155. }
  156. func (p *TSimpleJSONProtocol) WriteMessageEnd() error {
  157. return p.OutputListEnd()
  158. }
  159. func (p *TSimpleJSONProtocol) WriteStructBegin(name string) error {
  160. if e := p.OutputObjectBegin(); e != nil {
  161. return e
  162. }
  163. return nil
  164. }
  165. func (p *TSimpleJSONProtocol) WriteStructEnd() error {
  166. return p.OutputObjectEnd()
  167. }
  168. func (p *TSimpleJSONProtocol) WriteFieldBegin(name string, typeId TType, id int16) error {
  169. if e := p.WriteString(name); e != nil {
  170. return e
  171. }
  172. return nil
  173. }
  174. func (p *TSimpleJSONProtocol) WriteFieldEnd() error {
  175. //return p.OutputListEnd()
  176. return nil
  177. }
  178. func (p *TSimpleJSONProtocol) WriteFieldStop() error { return nil }
  179. func (p *TSimpleJSONProtocol) WriteMapBegin(keyType TType, valueType TType, size int) error {
  180. if e := p.OutputListBegin(); e != nil {
  181. return e
  182. }
  183. if e := p.WriteByte(int8(keyType)); e != nil {
  184. return e
  185. }
  186. if e := p.WriteByte(int8(valueType)); e != nil {
  187. return e
  188. }
  189. return p.WriteI32(int32(size))
  190. }
  191. func (p *TSimpleJSONProtocol) WriteMapEnd() error {
  192. return p.OutputListEnd()
  193. }
  194. func (p *TSimpleJSONProtocol) WriteListBegin(elemType TType, size int) error {
  195. return p.OutputElemListBegin(elemType, size)
  196. }
  197. func (p *TSimpleJSONProtocol) WriteListEnd() error {
  198. return p.OutputListEnd()
  199. }
  200. func (p *TSimpleJSONProtocol) WriteSetBegin(elemType TType, size int) error {
  201. return p.OutputElemListBegin(elemType, size)
  202. }
  203. func (p *TSimpleJSONProtocol) WriteSetEnd() error {
  204. return p.OutputListEnd()
  205. }
  206. func (p *TSimpleJSONProtocol) WriteBool(b bool) error {
  207. return p.OutputBool(b)
  208. }
  209. func (p *TSimpleJSONProtocol) WriteByte(b int8) error {
  210. return p.WriteI32(int32(b))
  211. }
  212. func (p *TSimpleJSONProtocol) WriteI16(v int16) error {
  213. return p.WriteI32(int32(v))
  214. }
  215. func (p *TSimpleJSONProtocol) WriteI32(v int32) error {
  216. return p.OutputI64(int64(v))
  217. }
  218. func (p *TSimpleJSONProtocol) WriteI64(v int64) error {
  219. return p.OutputI64(int64(v))
  220. }
  221. func (p *TSimpleJSONProtocol) WriteDouble(v float64) error {
  222. return p.OutputF64(v)
  223. }
  224. func (p *TSimpleJSONProtocol) WriteString(v string) error {
  225. return p.OutputString(v)
  226. }
  227. func (p *TSimpleJSONProtocol) WriteBinary(v []byte) error {
  228. // JSON library only takes in a string,
  229. // not an arbitrary byte array, to ensure bytes are transmitted
  230. // efficiently we must convert this into a valid JSON string
  231. // therefore we use base64 encoding to avoid excessive escaping/quoting
  232. if e := p.OutputPreValue(); e != nil {
  233. return e
  234. }
  235. if _, e := p.write(JSON_QUOTE_BYTES); e != nil {
  236. return NewTProtocolException(e)
  237. }
  238. writer := base64.NewEncoder(base64.StdEncoding, p.writer)
  239. if _, e := writer.Write(v); e != nil {
  240. p.writer.Reset(p.trans) // THRIFT-3735
  241. return NewTProtocolException(e)
  242. }
  243. if e := writer.Close(); e != nil {
  244. return NewTProtocolException(e)
  245. }
  246. if _, e := p.write(JSON_QUOTE_BYTES); e != nil {
  247. return NewTProtocolException(e)
  248. }
  249. return p.OutputPostValue()
  250. }
  251. // Reading methods.
  252. func (p *TSimpleJSONProtocol) ReadMessageBegin() (name string, typeId TMessageType, seqId int32, err error) {
  253. p.resetContextStack() // THRIFT-3735
  254. if isNull, err := p.ParseListBegin(); isNull || err != nil {
  255. return name, typeId, seqId, err
  256. }
  257. if name, err = p.ReadString(); err != nil {
  258. return name, typeId, seqId, err
  259. }
  260. bTypeId, err := p.ReadByte()
  261. typeId = TMessageType(bTypeId)
  262. if err != nil {
  263. return name, typeId, seqId, err
  264. }
  265. if seqId, err = p.ReadI32(); err != nil {
  266. return name, typeId, seqId, err
  267. }
  268. return name, typeId, seqId, nil
  269. }
  270. func (p *TSimpleJSONProtocol) ReadMessageEnd() error {
  271. return p.ParseListEnd()
  272. }
  273. func (p *TSimpleJSONProtocol) ReadStructBegin() (name string, err error) {
  274. _, err = p.ParseObjectStart()
  275. return "", err
  276. }
  277. func (p *TSimpleJSONProtocol) ReadStructEnd() error {
  278. return p.ParseObjectEnd()
  279. }
  280. func (p *TSimpleJSONProtocol) ReadFieldBegin() (string, TType, int16, error) {
  281. if err := p.ParsePreValue(); err != nil {
  282. return "", STOP, 0, err
  283. }
  284. b, _ := p.reader.Peek(1)
  285. if len(b) > 0 {
  286. switch b[0] {
  287. case JSON_RBRACE[0]:
  288. return "", STOP, 0, nil
  289. case JSON_QUOTE:
  290. p.reader.ReadByte()
  291. name, err := p.ParseStringBody()
  292. // simplejson is not meant to be read back into thrift
  293. // - see http://wiki.apache.org/thrift/ThriftUsageJava
  294. // - use JSON instead
  295. if err != nil {
  296. return name, STOP, 0, err
  297. }
  298. return name, STOP, -1, p.ParsePostValue()
  299. /*
  300. if err = p.ParsePostValue(); err != nil {
  301. return name, STOP, 0, err
  302. }
  303. if isNull, err := p.ParseListBegin(); isNull || err != nil {
  304. return name, STOP, 0, err
  305. }
  306. bType, err := p.ReadByte()
  307. thetype := TType(bType)
  308. if err != nil {
  309. return name, thetype, 0, err
  310. }
  311. id, err := p.ReadI16()
  312. return name, thetype, id, err
  313. */
  314. }
  315. e := fmt.Errorf("Expected \"}\" or '\"', but found: '%s'", string(b))
  316. return "", STOP, 0, NewTProtocolExceptionWithType(INVALID_DATA, e)
  317. }
  318. return "", STOP, 0, NewTProtocolException(io.EOF)
  319. }
  320. func (p *TSimpleJSONProtocol) ReadFieldEnd() error {
  321. return nil
  322. //return p.ParseListEnd()
  323. }
  324. func (p *TSimpleJSONProtocol) ReadMapBegin() (keyType TType, valueType TType, size int, e error) {
  325. if isNull, e := p.ParseListBegin(); isNull || e != nil {
  326. return VOID, VOID, 0, e
  327. }
  328. // read keyType
  329. bKeyType, e := p.ReadByte()
  330. keyType = TType(bKeyType)
  331. if e != nil {
  332. return keyType, valueType, size, e
  333. }
  334. // read valueType
  335. bValueType, e := p.ReadByte()
  336. valueType = TType(bValueType)
  337. if e != nil {
  338. return keyType, valueType, size, e
  339. }
  340. // read size
  341. iSize, err := p.ReadI64()
  342. size = int(iSize)
  343. return keyType, valueType, size, err
  344. }
  345. func (p *TSimpleJSONProtocol) ReadMapEnd() error {
  346. return p.ParseListEnd()
  347. }
  348. func (p *TSimpleJSONProtocol) ReadListBegin() (elemType TType, size int, e error) {
  349. return p.ParseElemListBegin()
  350. }
  351. func (p *TSimpleJSONProtocol) ReadListEnd() error {
  352. return p.ParseListEnd()
  353. }
  354. func (p *TSimpleJSONProtocol) ReadSetBegin() (elemType TType, size int, e error) {
  355. return p.ParseElemListBegin()
  356. }
  357. func (p *TSimpleJSONProtocol) ReadSetEnd() error {
  358. return p.ParseListEnd()
  359. }
  360. func (p *TSimpleJSONProtocol) ReadBool() (bool, error) {
  361. var value bool
  362. if err := p.ParsePreValue(); err != nil {
  363. return value, err
  364. }
  365. f, _ := p.reader.Peek(1)
  366. if len(f) > 0 {
  367. switch f[0] {
  368. case JSON_TRUE[0]:
  369. b := make([]byte, len(JSON_TRUE))
  370. _, err := p.reader.Read(b)
  371. if err != nil {
  372. return false, NewTProtocolException(err)
  373. }
  374. if string(b) == string(JSON_TRUE) {
  375. value = true
  376. } else {
  377. e := fmt.Errorf("Expected \"true\" but found: %s", string(b))
  378. return value, NewTProtocolExceptionWithType(INVALID_DATA, e)
  379. }
  380. break
  381. case JSON_FALSE[0]:
  382. b := make([]byte, len(JSON_FALSE))
  383. _, err := p.reader.Read(b)
  384. if err != nil {
  385. return false, NewTProtocolException(err)
  386. }
  387. if string(b) == string(JSON_FALSE) {
  388. value = false
  389. } else {
  390. e := fmt.Errorf("Expected \"false\" but found: %s", string(b))
  391. return value, NewTProtocolExceptionWithType(INVALID_DATA, e)
  392. }
  393. break
  394. case JSON_NULL[0]:
  395. b := make([]byte, len(JSON_NULL))
  396. _, err := p.reader.Read(b)
  397. if err != nil {
  398. return false, NewTProtocolException(err)
  399. }
  400. if string(b) == string(JSON_NULL) {
  401. value = false
  402. } else {
  403. e := fmt.Errorf("Expected \"null\" but found: %s", string(b))
  404. return value, NewTProtocolExceptionWithType(INVALID_DATA, e)
  405. }
  406. default:
  407. e := fmt.Errorf("Expected \"true\", \"false\", or \"null\" but found: %s", string(f))
  408. return value, NewTProtocolExceptionWithType(INVALID_DATA, e)
  409. }
  410. }
  411. return value, p.ParsePostValue()
  412. }
  413. func (p *TSimpleJSONProtocol) ReadByte() (int8, error) {
  414. v, err := p.ReadI64()
  415. return int8(v), err
  416. }
  417. func (p *TSimpleJSONProtocol) ReadI16() (int16, error) {
  418. v, err := p.ReadI64()
  419. return int16(v), err
  420. }
  421. func (p *TSimpleJSONProtocol) ReadI32() (int32, error) {
  422. v, err := p.ReadI64()
  423. return int32(v), err
  424. }
  425. func (p *TSimpleJSONProtocol) ReadI64() (int64, error) {
  426. v, _, err := p.ParseI64()
  427. return v, err
  428. }
  429. func (p *TSimpleJSONProtocol) ReadDouble() (float64, error) {
  430. v, _, err := p.ParseF64()
  431. return v, err
  432. }
  433. func (p *TSimpleJSONProtocol) ReadString() (string, error) {
  434. var v string
  435. if err := p.ParsePreValue(); err != nil {
  436. return v, err
  437. }
  438. f, _ := p.reader.Peek(1)
  439. if len(f) > 0 && f[0] == JSON_QUOTE {
  440. p.reader.ReadByte()
  441. value, err := p.ParseStringBody()
  442. v = value
  443. if err != nil {
  444. return v, err
  445. }
  446. } else if len(f) > 0 && f[0] == JSON_NULL[0] {
  447. b := make([]byte, len(JSON_NULL))
  448. _, err := p.reader.Read(b)
  449. if err != nil {
  450. return v, NewTProtocolException(err)
  451. }
  452. if string(b) != string(JSON_NULL) {
  453. e := fmt.Errorf("Expected a JSON string, found unquoted data started with %s", string(b))
  454. return v, NewTProtocolExceptionWithType(INVALID_DATA, e)
  455. }
  456. } else {
  457. e := fmt.Errorf("Expected a JSON string, found unquoted data started with %s", string(f))
  458. return v, NewTProtocolExceptionWithType(INVALID_DATA, e)
  459. }
  460. return v, p.ParsePostValue()
  461. }
  462. func (p *TSimpleJSONProtocol) ReadBinary() ([]byte, error) {
  463. var v []byte
  464. if err := p.ParsePreValue(); err != nil {
  465. return nil, err
  466. }
  467. f, _ := p.reader.Peek(1)
  468. if len(f) > 0 && f[0] == JSON_QUOTE {
  469. p.reader.ReadByte()
  470. value, err := p.ParseBase64EncodedBody()
  471. v = value
  472. if err != nil {
  473. return v, err
  474. }
  475. } else if len(f) > 0 && f[0] == JSON_NULL[0] {
  476. b := make([]byte, len(JSON_NULL))
  477. _, err := p.reader.Read(b)
  478. if err != nil {
  479. return v, NewTProtocolException(err)
  480. }
  481. if string(b) != string(JSON_NULL) {
  482. e := fmt.Errorf("Expected a JSON string, found unquoted data started with %s", string(b))
  483. return v, NewTProtocolExceptionWithType(INVALID_DATA, e)
  484. }
  485. } else {
  486. e := fmt.Errorf("Expected a JSON string, found unquoted data started with %s", string(f))
  487. return v, NewTProtocolExceptionWithType(INVALID_DATA, e)
  488. }
  489. return v, p.ParsePostValue()
  490. }
  491. func (p *TSimpleJSONProtocol) Flush() (err error) {
  492. return NewTProtocolException(p.writer.Flush())
  493. }
  494. func (p *TSimpleJSONProtocol) Skip(fieldType TType) (err error) {
  495. return SkipDefaultDepth(p, fieldType)
  496. }
  497. func (p *TSimpleJSONProtocol) Transport() TTransport {
  498. return p.trans
  499. }
  500. func (p *TSimpleJSONProtocol) OutputPreValue() error {
  501. cxt := _ParseContext(p.dumpContext[len(p.dumpContext)-1])
  502. switch cxt {
  503. case _CONTEXT_IN_LIST, _CONTEXT_IN_OBJECT_NEXT_KEY:
  504. if _, e := p.write(JSON_COMMA); e != nil {
  505. return NewTProtocolException(e)
  506. }
  507. break
  508. case _CONTEXT_IN_OBJECT_NEXT_VALUE:
  509. if _, e := p.write(JSON_COLON); e != nil {
  510. return NewTProtocolException(e)
  511. }
  512. break
  513. }
  514. return nil
  515. }
  516. func (p *TSimpleJSONProtocol) OutputPostValue() error {
  517. cxt := _ParseContext(p.dumpContext[len(p.dumpContext)-1])
  518. switch cxt {
  519. case _CONTEXT_IN_LIST_FIRST:
  520. p.dumpContext = p.dumpContext[:len(p.dumpContext)-1]
  521. p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_LIST))
  522. break
  523. case _CONTEXT_IN_OBJECT_FIRST:
  524. p.dumpContext = p.dumpContext[:len(p.dumpContext)-1]
  525. p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_OBJECT_NEXT_VALUE))
  526. break
  527. case _CONTEXT_IN_OBJECT_NEXT_KEY:
  528. p.dumpContext = p.dumpContext[:len(p.dumpContext)-1]
  529. p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_OBJECT_NEXT_VALUE))
  530. break
  531. case _CONTEXT_IN_OBJECT_NEXT_VALUE:
  532. p.dumpContext = p.dumpContext[:len(p.dumpContext)-1]
  533. p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_OBJECT_NEXT_KEY))
  534. break
  535. }
  536. return nil
  537. }
  538. func (p *TSimpleJSONProtocol) OutputBool(value bool) error {
  539. if e := p.OutputPreValue(); e != nil {
  540. return e
  541. }
  542. var v string
  543. if value {
  544. v = string(JSON_TRUE)
  545. } else {
  546. v = string(JSON_FALSE)
  547. }
  548. switch _ParseContext(p.dumpContext[len(p.dumpContext)-1]) {
  549. case _CONTEXT_IN_OBJECT_FIRST, _CONTEXT_IN_OBJECT_NEXT_KEY:
  550. v = jsonQuote(v)
  551. default:
  552. }
  553. if e := p.OutputStringData(v); e != nil {
  554. return e
  555. }
  556. return p.OutputPostValue()
  557. }
  558. func (p *TSimpleJSONProtocol) OutputNull() error {
  559. if e := p.OutputPreValue(); e != nil {
  560. return e
  561. }
  562. if _, e := p.write(JSON_NULL); e != nil {
  563. return NewTProtocolException(e)
  564. }
  565. return p.OutputPostValue()
  566. }
  567. func (p *TSimpleJSONProtocol) OutputF64(value float64) error {
  568. if e := p.OutputPreValue(); e != nil {
  569. return e
  570. }
  571. var v string
  572. if math.IsNaN(value) {
  573. v = string(JSON_QUOTE) + JSON_NAN + string(JSON_QUOTE)
  574. } else if math.IsInf(value, 1) {
  575. v = string(JSON_QUOTE) + JSON_INFINITY + string(JSON_QUOTE)
  576. } else if math.IsInf(value, -1) {
  577. v = string(JSON_QUOTE) + JSON_NEGATIVE_INFINITY + string(JSON_QUOTE)
  578. } else {
  579. v = strconv.FormatFloat(value, 'g', -1, 64)
  580. switch _ParseContext(p.dumpContext[len(p.dumpContext)-1]) {
  581. case _CONTEXT_IN_OBJECT_FIRST, _CONTEXT_IN_OBJECT_NEXT_KEY:
  582. v = string(JSON_QUOTE) + v + string(JSON_QUOTE)
  583. default:
  584. }
  585. }
  586. if e := p.OutputStringData(v); e != nil {
  587. return e
  588. }
  589. return p.OutputPostValue()
  590. }
  591. func (p *TSimpleJSONProtocol) OutputI64(value int64) error {
  592. if e := p.OutputPreValue(); e != nil {
  593. return e
  594. }
  595. v := strconv.FormatInt(value, 10)
  596. switch _ParseContext(p.dumpContext[len(p.dumpContext)-1]) {
  597. case _CONTEXT_IN_OBJECT_FIRST, _CONTEXT_IN_OBJECT_NEXT_KEY:
  598. v = jsonQuote(v)
  599. default:
  600. }
  601. if e := p.OutputStringData(v); e != nil {
  602. return e
  603. }
  604. return p.OutputPostValue()
  605. }
  606. func (p *TSimpleJSONProtocol) OutputString(s string) error {
  607. if e := p.OutputPreValue(); e != nil {
  608. return e
  609. }
  610. if e := p.OutputStringData(jsonQuote(s)); e != nil {
  611. return e
  612. }
  613. return p.OutputPostValue()
  614. }
  615. func (p *TSimpleJSONProtocol) OutputStringData(s string) error {
  616. _, e := p.write([]byte(s))
  617. return NewTProtocolException(e)
  618. }
  619. func (p *TSimpleJSONProtocol) OutputObjectBegin() error {
  620. if e := p.OutputPreValue(); e != nil {
  621. return e
  622. }
  623. if _, e := p.write(JSON_LBRACE); e != nil {
  624. return NewTProtocolException(e)
  625. }
  626. p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_OBJECT_FIRST))
  627. return nil
  628. }
  629. func (p *TSimpleJSONProtocol) OutputObjectEnd() error {
  630. if _, e := p.write(JSON_RBRACE); e != nil {
  631. return NewTProtocolException(e)
  632. }
  633. p.dumpContext = p.dumpContext[:len(p.dumpContext)-1]
  634. if e := p.OutputPostValue(); e != nil {
  635. return e
  636. }
  637. return nil
  638. }
  639. func (p *TSimpleJSONProtocol) OutputListBegin() error {
  640. if e := p.OutputPreValue(); e != nil {
  641. return e
  642. }
  643. if _, e := p.write(JSON_LBRACKET); e != nil {
  644. return NewTProtocolException(e)
  645. }
  646. p.dumpContext = append(p.dumpContext, int(_CONTEXT_IN_LIST_FIRST))
  647. return nil
  648. }
  649. func (p *TSimpleJSONProtocol) OutputListEnd() error {
  650. if _, e := p.write(JSON_RBRACKET); e != nil {
  651. return NewTProtocolException(e)
  652. }
  653. p.dumpContext = p.dumpContext[:len(p.dumpContext)-1]
  654. if e := p.OutputPostValue(); e != nil {
  655. return e
  656. }
  657. return nil
  658. }
  659. func (p *TSimpleJSONProtocol) OutputElemListBegin(elemType TType, size int) error {
  660. if e := p.OutputListBegin(); e != nil {
  661. return e
  662. }
  663. if e := p.WriteByte(int8(elemType)); e != nil {
  664. return e
  665. }
  666. if e := p.WriteI64(int64(size)); e != nil {
  667. return e
  668. }
  669. return nil
  670. }
  671. func (p *TSimpleJSONProtocol) ParsePreValue() error {
  672. if e := p.readNonSignificantWhitespace(); e != nil {
  673. return NewTProtocolException(e)
  674. }
  675. cxt := _ParseContext(p.parseContextStack[len(p.parseContextStack)-1])
  676. b, _ := p.reader.Peek(1)
  677. switch cxt {
  678. case _CONTEXT_IN_LIST:
  679. if len(b) > 0 {
  680. switch b[0] {
  681. case JSON_RBRACKET[0]:
  682. return nil
  683. case JSON_COMMA[0]:
  684. p.reader.ReadByte()
  685. if e := p.readNonSignificantWhitespace(); e != nil {
  686. return NewTProtocolException(e)
  687. }
  688. return nil
  689. default:
  690. e := fmt.Errorf("Expected \"]\" or \",\" in list context, but found \"%s\"", string(b))
  691. return NewTProtocolExceptionWithType(INVALID_DATA, e)
  692. }
  693. }
  694. break
  695. case _CONTEXT_IN_OBJECT_NEXT_KEY:
  696. if len(b) > 0 {
  697. switch b[0] {
  698. case JSON_RBRACE[0]:
  699. return nil
  700. case JSON_COMMA[0]:
  701. p.reader.ReadByte()
  702. if e := p.readNonSignificantWhitespace(); e != nil {
  703. return NewTProtocolException(e)
  704. }
  705. return nil
  706. default:
  707. e := fmt.Errorf("Expected \"}\" or \",\" in object context, but found \"%s\"", string(b))
  708. return NewTProtocolExceptionWithType(INVALID_DATA, e)
  709. }
  710. }
  711. break
  712. case _CONTEXT_IN_OBJECT_NEXT_VALUE:
  713. if len(b) > 0 {
  714. switch b[0] {
  715. case JSON_COLON[0]:
  716. p.reader.ReadByte()
  717. if e := p.readNonSignificantWhitespace(); e != nil {
  718. return NewTProtocolException(e)
  719. }
  720. return nil
  721. default:
  722. e := fmt.Errorf("Expected \":\" in object context, but found \"%s\"", string(b))
  723. return NewTProtocolExceptionWithType(INVALID_DATA, e)
  724. }
  725. }
  726. break
  727. }
  728. return nil
  729. }
  730. func (p *TSimpleJSONProtocol) ParsePostValue() error {
  731. if e := p.readNonSignificantWhitespace(); e != nil {
  732. return NewTProtocolException(e)
  733. }
  734. cxt := _ParseContext(p.parseContextStack[len(p.parseContextStack)-1])
  735. switch cxt {
  736. case _CONTEXT_IN_LIST_FIRST:
  737. p.parseContextStack = p.parseContextStack[:len(p.parseContextStack)-1]
  738. p.parseContextStack = append(p.parseContextStack, int(_CONTEXT_IN_LIST))
  739. break
  740. case _CONTEXT_IN_OBJECT_FIRST, _CONTEXT_IN_OBJECT_NEXT_KEY:
  741. p.parseContextStack = p.parseContextStack[:len(p.parseContextStack)-1]
  742. p.parseContextStack = append(p.parseContextStack, int(_CONTEXT_IN_OBJECT_NEXT_VALUE))
  743. break
  744. case _CONTEXT_IN_OBJECT_NEXT_VALUE:
  745. p.parseContextStack = p.parseContextStack[:len(p.parseContextStack)-1]
  746. p.parseContextStack = append(p.parseContextStack, int(_CONTEXT_IN_OBJECT_NEXT_KEY))
  747. break
  748. }
  749. return nil
  750. }
  751. func (p *TSimpleJSONProtocol) readNonSignificantWhitespace() error {
  752. for {
  753. b, _ := p.reader.Peek(1)
  754. if len(b) < 1 {
  755. return nil
  756. }
  757. switch b[0] {
  758. case ' ', '\r', '\n', '\t':
  759. p.reader.ReadByte()
  760. continue
  761. default:
  762. break
  763. }
  764. break
  765. }
  766. return nil
  767. }
  768. func (p *TSimpleJSONProtocol) ParseStringBody() (string, error) {
  769. line, err := p.reader.ReadString(JSON_QUOTE)
  770. if err != nil {
  771. return "", NewTProtocolException(err)
  772. }
  773. l := len(line)
  774. // count number of escapes to see if we need to keep going
  775. i := 1
  776. for ; i < l; i++ {
  777. if line[l-i-1] != '\\' {
  778. break
  779. }
  780. }
  781. if i&0x01 == 1 {
  782. v, ok := jsonUnquote(string(JSON_QUOTE) + line)
  783. if !ok {
  784. return "", NewTProtocolException(err)
  785. }
  786. return v, nil
  787. }
  788. s, err := p.ParseQuotedStringBody()
  789. if err != nil {
  790. return "", NewTProtocolException(err)
  791. }
  792. str := string(JSON_QUOTE) + line + s
  793. v, ok := jsonUnquote(str)
  794. if !ok {
  795. e := fmt.Errorf("Unable to parse as JSON string %s", str)
  796. return "", NewTProtocolExceptionWithType(INVALID_DATA, e)
  797. }
  798. return v, nil
  799. }
  800. func (p *TSimpleJSONProtocol) ParseQuotedStringBody() (string, error) {
  801. line, err := p.reader.ReadString(JSON_QUOTE)
  802. if err != nil {
  803. return "", NewTProtocolException(err)
  804. }
  805. l := len(line)
  806. // count number of escapes to see if we need to keep going
  807. i := 1
  808. for ; i < l; i++ {
  809. if line[l-i-1] != '\\' {
  810. break
  811. }
  812. }
  813. if i&0x01 == 1 {
  814. return line, nil
  815. }
  816. s, err := p.ParseQuotedStringBody()
  817. if err != nil {
  818. return "", NewTProtocolException(err)
  819. }
  820. v := line + s
  821. return v, nil
  822. }
  823. func (p *TSimpleJSONProtocol) ParseBase64EncodedBody() ([]byte, error) {
  824. line, err := p.reader.ReadBytes(JSON_QUOTE)
  825. if err != nil {
  826. return line, NewTProtocolException(err)
  827. }
  828. line2 := line[0 : len(line)-1]
  829. l := len(line2)
  830. if (l % 4) != 0 {
  831. pad := 4 - (l % 4)
  832. fill := [...]byte{'=', '=', '='}
  833. line2 = append(line2, fill[:pad]...)
  834. l = len(line2)
  835. }
  836. output := make([]byte, base64.StdEncoding.DecodedLen(l))
  837. n, err := base64.StdEncoding.Decode(output, line2)
  838. return output[0:n], NewTProtocolException(err)
  839. }
  840. func (p *TSimpleJSONProtocol) ParseI64() (int64, bool, error) {
  841. if err := p.ParsePreValue(); err != nil {
  842. return 0, false, err
  843. }
  844. var value int64
  845. var isnull bool
  846. if p.safePeekContains(JSON_NULL) {
  847. p.reader.Read(make([]byte, len(JSON_NULL)))
  848. isnull = true
  849. } else {
  850. num, err := p.readNumeric()
  851. isnull = (num == nil)
  852. if !isnull {
  853. value = num.Int64()
  854. }
  855. if err != nil {
  856. return value, isnull, err
  857. }
  858. }
  859. return value, isnull, p.ParsePostValue()
  860. }
  861. func (p *TSimpleJSONProtocol) ParseF64() (float64, bool, error) {
  862. if err := p.ParsePreValue(); err != nil {
  863. return 0, false, err
  864. }
  865. var value float64
  866. var isnull bool
  867. if p.safePeekContains(JSON_NULL) {
  868. p.reader.Read(make([]byte, len(JSON_NULL)))
  869. isnull = true
  870. } else {
  871. num, err := p.readNumeric()
  872. isnull = (num == nil)
  873. if !isnull {
  874. value = num.Float64()
  875. }
  876. if err != nil {
  877. return value, isnull, err
  878. }
  879. }
  880. return value, isnull, p.ParsePostValue()
  881. }
  882. func (p *TSimpleJSONProtocol) ParseObjectStart() (bool, error) {
  883. if err := p.ParsePreValue(); err != nil {
  884. return false, err
  885. }
  886. var b []byte
  887. b, err := p.reader.Peek(1)
  888. if err != nil {
  889. return false, err
  890. }
  891. if len(b) > 0 && b[0] == JSON_LBRACE[0] {
  892. p.reader.ReadByte()
  893. p.parseContextStack = append(p.parseContextStack, int(_CONTEXT_IN_OBJECT_FIRST))
  894. return false, nil
  895. } else if p.safePeekContains(JSON_NULL) {
  896. return true, nil
  897. }
  898. e := fmt.Errorf("Expected '{' or null, but found '%s'", string(b))
  899. return false, NewTProtocolExceptionWithType(INVALID_DATA, e)
  900. }
  901. func (p *TSimpleJSONProtocol) ParseObjectEnd() error {
  902. if isNull, err := p.readIfNull(); isNull || err != nil {
  903. return err
  904. }
  905. cxt := _ParseContext(p.parseContextStack[len(p.parseContextStack)-1])
  906. if (cxt != _CONTEXT_IN_OBJECT_FIRST) && (cxt != _CONTEXT_IN_OBJECT_NEXT_KEY) {
  907. e := fmt.Errorf("Expected to be in the Object Context, but not in Object Context (%d)", cxt)
  908. return NewTProtocolExceptionWithType(INVALID_DATA, e)
  909. }
  910. line, err := p.reader.ReadString(JSON_RBRACE[0])
  911. if err != nil {
  912. return NewTProtocolException(err)
  913. }
  914. for _, char := range line {
  915. switch char {
  916. default:
  917. e := fmt.Errorf("Expecting end of object \"}\", but found: \"%s\"", line)
  918. return NewTProtocolExceptionWithType(INVALID_DATA, e)
  919. case ' ', '\n', '\r', '\t', '}':
  920. break
  921. }
  922. }
  923. p.parseContextStack = p.parseContextStack[:len(p.parseContextStack)-1]
  924. return p.ParsePostValue()
  925. }
  926. func (p *TSimpleJSONProtocol) ParseListBegin() (isNull bool, err error) {
  927. if e := p.ParsePreValue(); e != nil {
  928. return false, e
  929. }
  930. var b []byte
  931. b, err = p.reader.Peek(1)
  932. if err != nil {
  933. return false, err
  934. }
  935. if len(b) >= 1 && b[0] == JSON_LBRACKET[0] {
  936. p.parseContextStack = append(p.parseContextStack, int(_CONTEXT_IN_LIST_FIRST))
  937. p.reader.ReadByte()
  938. isNull = false
  939. } else if p.safePeekContains(JSON_NULL) {
  940. isNull = true
  941. } else {
  942. err = fmt.Errorf("Expected \"null\" or \"[\", received %q", b)
  943. }
  944. return isNull, NewTProtocolExceptionWithType(INVALID_DATA, err)
  945. }
  946. func (p *TSimpleJSONProtocol) ParseElemListBegin() (elemType TType, size int, e error) {
  947. if isNull, e := p.ParseListBegin(); isNull || e != nil {
  948. return VOID, 0, e
  949. }
  950. bElemType, err := p.ReadByte()
  951. elemType = TType(bElemType)
  952. if err != nil {
  953. return elemType, size, err
  954. }
  955. nSize, err2 := p.ReadI64()
  956. size = int(nSize)
  957. return elemType, size, err2
  958. }
  959. func (p *TSimpleJSONProtocol) ParseListEnd() error {
  960. if isNull, err := p.readIfNull(); isNull || err != nil {
  961. return err
  962. }
  963. cxt := _ParseContext(p.parseContextStack[len(p.parseContextStack)-1])
  964. if cxt != _CONTEXT_IN_LIST {
  965. e := fmt.Errorf("Expected to be in the List Context, but not in List Context (%d)", cxt)
  966. return NewTProtocolExceptionWithType(INVALID_DATA, e)
  967. }
  968. line, err := p.reader.ReadString(JSON_RBRACKET[0])
  969. if err != nil {
  970. return NewTProtocolException(err)
  971. }
  972. for _, char := range line {
  973. switch char {
  974. default:
  975. e := fmt.Errorf("Expecting end of list \"]\", but found: \"%s\"", line)
  976. return NewTProtocolExceptionWithType(INVALID_DATA, e)
  977. case ' ', '\n', '\r', '\t', rune(JSON_RBRACKET[0]):
  978. break
  979. }
  980. }
  981. p.parseContextStack = p.parseContextStack[:len(p.parseContextStack)-1]
  982. if _ParseContext(p.parseContextStack[len(p.parseContextStack)-1]) == _CONTEXT_IN_TOPLEVEL {
  983. return nil
  984. }
  985. return p.ParsePostValue()
  986. }
  987. func (p *TSimpleJSONProtocol) readSingleValue() (interface{}, TType, error) {
  988. e := p.readNonSignificantWhitespace()
  989. if e != nil {
  990. return nil, VOID, NewTProtocolException(e)
  991. }
  992. b, e := p.reader.Peek(1)
  993. if len(b) > 0 {
  994. c := b[0]
  995. switch c {
  996. case JSON_NULL[0]:
  997. buf := make([]byte, len(JSON_NULL))
  998. _, e := p.reader.Read(buf)
  999. if e != nil {
  1000. return nil, VOID, NewTProtocolException(e)
  1001. }
  1002. if string(JSON_NULL) != string(buf) {
  1003. e = mismatch(string(JSON_NULL), string(buf))
  1004. return nil, VOID, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1005. }
  1006. return nil, VOID, nil
  1007. case JSON_QUOTE:
  1008. p.reader.ReadByte()
  1009. v, e := p.ParseStringBody()
  1010. if e != nil {
  1011. return v, UTF8, NewTProtocolException(e)
  1012. }
  1013. if v == JSON_INFINITY {
  1014. return INFINITY, DOUBLE, nil
  1015. } else if v == JSON_NEGATIVE_INFINITY {
  1016. return NEGATIVE_INFINITY, DOUBLE, nil
  1017. } else if v == JSON_NAN {
  1018. return NAN, DOUBLE, nil
  1019. }
  1020. return v, UTF8, nil
  1021. case JSON_TRUE[0]:
  1022. buf := make([]byte, len(JSON_TRUE))
  1023. _, e := p.reader.Read(buf)
  1024. if e != nil {
  1025. return true, BOOL, NewTProtocolException(e)
  1026. }
  1027. if string(JSON_TRUE) != string(buf) {
  1028. e := mismatch(string(JSON_TRUE), string(buf))
  1029. return true, BOOL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1030. }
  1031. return true, BOOL, nil
  1032. case JSON_FALSE[0]:
  1033. buf := make([]byte, len(JSON_FALSE))
  1034. _, e := p.reader.Read(buf)
  1035. if e != nil {
  1036. return false, BOOL, NewTProtocolException(e)
  1037. }
  1038. if string(JSON_FALSE) != string(buf) {
  1039. e := mismatch(string(JSON_FALSE), string(buf))
  1040. return false, BOOL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1041. }
  1042. return false, BOOL, nil
  1043. case JSON_LBRACKET[0]:
  1044. _, e := p.reader.ReadByte()
  1045. return make([]interface{}, 0), LIST, NewTProtocolException(e)
  1046. case JSON_LBRACE[0]:
  1047. _, e := p.reader.ReadByte()
  1048. return make(map[string]interface{}), STRUCT, NewTProtocolException(e)
  1049. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'e', 'E', '.', '+', '-', JSON_INFINITY[0], JSON_NAN[0]:
  1050. // assume numeric
  1051. v, e := p.readNumeric()
  1052. return v, DOUBLE, e
  1053. default:
  1054. e := fmt.Errorf("Expected element in list but found '%s' while parsing JSON.", string(c))
  1055. return nil, VOID, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1056. }
  1057. }
  1058. e = fmt.Errorf("Cannot read a single element while parsing JSON.")
  1059. return nil, VOID, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1060. }
  1061. func (p *TSimpleJSONProtocol) readIfNull() (bool, error) {
  1062. cont := true
  1063. for cont {
  1064. b, _ := p.reader.Peek(1)
  1065. if len(b) < 1 {
  1066. return false, nil
  1067. }
  1068. switch b[0] {
  1069. default:
  1070. return false, nil
  1071. case JSON_NULL[0]:
  1072. cont = false
  1073. break
  1074. case ' ', '\n', '\r', '\t':
  1075. p.reader.ReadByte()
  1076. break
  1077. }
  1078. }
  1079. if p.safePeekContains(JSON_NULL) {
  1080. p.reader.Read(make([]byte, len(JSON_NULL)))
  1081. return true, nil
  1082. }
  1083. return false, nil
  1084. }
  1085. func (p *TSimpleJSONProtocol) readQuoteIfNext() {
  1086. b, _ := p.reader.Peek(1)
  1087. if len(b) > 0 && b[0] == JSON_QUOTE {
  1088. p.reader.ReadByte()
  1089. }
  1090. }
  1091. func (p *TSimpleJSONProtocol) readNumeric() (Numeric, error) {
  1092. isNull, err := p.readIfNull()
  1093. if isNull || err != nil {
  1094. return NUMERIC_NULL, err
  1095. }
  1096. hasDecimalPoint := false
  1097. nextCanBeSign := true
  1098. hasE := false
  1099. MAX_LEN := 40
  1100. buf := bytes.NewBuffer(make([]byte, 0, MAX_LEN))
  1101. continueFor := true
  1102. inQuotes := false
  1103. for continueFor {
  1104. c, err := p.reader.ReadByte()
  1105. if err != nil {
  1106. if err == io.EOF {
  1107. break
  1108. }
  1109. return NUMERIC_NULL, NewTProtocolException(err)
  1110. }
  1111. switch c {
  1112. case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  1113. buf.WriteByte(c)
  1114. nextCanBeSign = false
  1115. case '.':
  1116. if hasDecimalPoint {
  1117. e := fmt.Errorf("Unable to parse number with multiple decimal points '%s.'", buf.String())
  1118. return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1119. }
  1120. if hasE {
  1121. e := fmt.Errorf("Unable to parse number with decimal points in the exponent '%s.'", buf.String())
  1122. return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1123. }
  1124. buf.WriteByte(c)
  1125. hasDecimalPoint, nextCanBeSign = true, false
  1126. case 'e', 'E':
  1127. if hasE {
  1128. e := fmt.Errorf("Unable to parse number with multiple exponents '%s%c'", buf.String(), c)
  1129. return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1130. }
  1131. buf.WriteByte(c)
  1132. hasE, nextCanBeSign = true, true
  1133. case '-', '+':
  1134. if !nextCanBeSign {
  1135. e := fmt.Errorf("Negative sign within number")
  1136. return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1137. }
  1138. buf.WriteByte(c)
  1139. nextCanBeSign = false
  1140. case ' ', 0, '\t', '\n', '\r', JSON_RBRACE[0], JSON_RBRACKET[0], JSON_COMMA[0], JSON_COLON[0]:
  1141. p.reader.UnreadByte()
  1142. continueFor = false
  1143. case JSON_NAN[0]:
  1144. if buf.Len() == 0 {
  1145. buffer := make([]byte, len(JSON_NAN))
  1146. buffer[0] = c
  1147. _, e := p.reader.Read(buffer[1:])
  1148. if e != nil {
  1149. return NUMERIC_NULL, NewTProtocolException(e)
  1150. }
  1151. if JSON_NAN != string(buffer) {
  1152. e := mismatch(JSON_NAN, string(buffer))
  1153. return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1154. }
  1155. if inQuotes {
  1156. p.readQuoteIfNext()
  1157. }
  1158. return NAN, nil
  1159. } else {
  1160. e := fmt.Errorf("Unable to parse number starting with character '%c'", c)
  1161. return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1162. }
  1163. case JSON_INFINITY[0]:
  1164. if buf.Len() == 0 || (buf.Len() == 1 && buf.Bytes()[0] == '+') {
  1165. buffer := make([]byte, len(JSON_INFINITY))
  1166. buffer[0] = c
  1167. _, e := p.reader.Read(buffer[1:])
  1168. if e != nil {
  1169. return NUMERIC_NULL, NewTProtocolException(e)
  1170. }
  1171. if JSON_INFINITY != string(buffer) {
  1172. e := mismatch(JSON_INFINITY, string(buffer))
  1173. return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1174. }
  1175. if inQuotes {
  1176. p.readQuoteIfNext()
  1177. }
  1178. return INFINITY, nil
  1179. } else if buf.Len() == 1 && buf.Bytes()[0] == JSON_NEGATIVE_INFINITY[0] {
  1180. buffer := make([]byte, len(JSON_NEGATIVE_INFINITY))
  1181. buffer[0] = JSON_NEGATIVE_INFINITY[0]
  1182. buffer[1] = c
  1183. _, e := p.reader.Read(buffer[2:])
  1184. if e != nil {
  1185. return NUMERIC_NULL, NewTProtocolException(e)
  1186. }
  1187. if JSON_NEGATIVE_INFINITY != string(buffer) {
  1188. e := mismatch(JSON_NEGATIVE_INFINITY, string(buffer))
  1189. return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1190. }
  1191. if inQuotes {
  1192. p.readQuoteIfNext()
  1193. }
  1194. return NEGATIVE_INFINITY, nil
  1195. } else {
  1196. e := fmt.Errorf("Unable to parse number starting with character '%c' due to existing buffer %s", c, buf.String())
  1197. return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1198. }
  1199. case JSON_QUOTE:
  1200. if !inQuotes {
  1201. inQuotes = true
  1202. } else {
  1203. break
  1204. }
  1205. default:
  1206. e := fmt.Errorf("Unable to parse number starting with character '%c'", c)
  1207. return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1208. }
  1209. }
  1210. if buf.Len() == 0 {
  1211. e := fmt.Errorf("Unable to parse number from empty string ''")
  1212. return NUMERIC_NULL, NewTProtocolExceptionWithType(INVALID_DATA, e)
  1213. }
  1214. return NewNumericFromJSONString(buf.String(), false), nil
  1215. }
  1216. // Safely peeks into the buffer, reading only what is necessary
  1217. func (p *TSimpleJSONProtocol) safePeekContains(b []byte) bool {
  1218. for i := 0; i < len(b); i++ {
  1219. a, _ := p.reader.Peek(i + 1)
  1220. if len(a) == 0 || a[i] != b[i] {
  1221. return false
  1222. }
  1223. }
  1224. return true
  1225. }
  1226. // Reset the context stack to its initial state.
  1227. func (p *TSimpleJSONProtocol) resetContextStack() {
  1228. p.parseContextStack = []int{int(_CONTEXT_IN_TOPLEVEL)}
  1229. p.dumpContext = []int{int(_CONTEXT_IN_TOPLEVEL)}
  1230. }
  1231. func (p *TSimpleJSONProtocol) write(b []byte) (int, error) {
  1232. n, err := p.writer.Write(b)
  1233. if err != nil {
  1234. p.writer.Reset(p.trans) // THRIFT-3735
  1235. }
  1236. return n, err
  1237. }