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