simple_json_protocol.go 36 KB

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