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