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