simple_json_protocol.go 34 KB

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