class Thrift::JsonProtocol
Attributes
Public Class Methods
Source
# File lib/thrift/protocol/json_protocol.rb 152 def initialize(trans, max_string_size: DEFAULT_MAX_STRING_SIZE) 153 BaseProtocol.validate_max_string_size(max_string_size) 154 super(trans) 155 @max_string_size = max_string_size 156 @context = JSONContext.new 157 @contexts = Array.new 158 @reader = LookaheadReader.new(trans) 159 end
Thrift::BaseProtocol::new
Source
# File lib/thrift/protocol/json_protocol.rb 215 def self.read_syntax_char(reader, ch) 216 ch2 = reader.read 217 if (ch2 != ch) 218 raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected '#{ch}' got '#{ch2}'.") 219 end 220 end
Read 1 character from the trans and verify that it is the expected character ch. Throw a protocol exception if it is not.
Public Instance Methods
Source
# File lib/thrift/protocol/json_protocol.rb 192 def get_type_id_for_type_name(name) 193 case name 194 when "tf" then Types::BOOL 195 when "i8" then Types::BYTE 196 when "i16" then Types::I16 197 when "i32" then Types::I32 198 when "i64" then Types::I64 199 when "dbl" then Types::DOUBLE 200 when "str" then Types::STRING 201 when "rec" then Types::STRUCT 202 when "map" then Types::MAP 203 when "set" then Types::SET 204 when "lst" then Types::LIST 205 when "uid" then Types::UUID 206 else 207 raise ProtocolException.new(ProtocolException::INVALID_DATA, "Unknown type name: #{name.inspect}") 208 end 209 end
Source
# File lib/thrift/protocol/json_protocol.rb 161 def get_type_name_for_type_id(id) 162 case id 163 when Types::BOOL 164 "tf" 165 when Types::BYTE 166 "i8" 167 when Types::I16 168 "i16" 169 when Types::I32 170 "i32" 171 when Types::I64 172 "i64" 173 when Types::DOUBLE 174 "dbl" 175 when Types::STRING 176 "str" 177 when Types::STRUCT 178 "rec" 179 when Types::MAP 180 "map" 181 when Types::SET 182 "set" 183 when Types::LIST 184 "lst" 185 when Types::UUID 186 "uid" 187 else 188 raise ProtocolException.new(ProtocolException::INVALID_DATA, "Unknown type id: #{id}") 189 end 190 end
Source
# File lib/thrift/protocol/json_protocol.rb 223 def is_json_numeric(ch) 224 case ch 225 when "+", "-", ".", "0".."9", "E", "e" 226 true 227 else 228 false 229 end 230 end
Return true if the character ch is in [-+0-9.Ee]; false otherwise
Source
# File lib/thrift/protocol/json_protocol.rb 237 def pop_context 238 @context = @contexts.pop 239 end
Source
# File lib/thrift/protocol/json_protocol.rb 232 def push_context(context) 233 @contexts.push(@context) 234 @context = context 235 end
Source
# File lib/thrift/protocol/json_protocol.rb 778 def read_binary 779 read_json_base64 780 end
Source
# File lib/thrift/protocol/json_protocol.rb 749 def read_bool 750 byte = read_byte 751 byte != 0 752 end
Source
# File lib/thrift/protocol/json_protocol.rb 754 def read_byte 755 read_json_integer 756 end
Source
# File lib/thrift/protocol/json_protocol.rb 770 def read_double 771 read_json_double 772 end
Source
# File lib/thrift/protocol/json_protocol.rb 693 def read_field_begin 694 # Check if we hit the end of the list 695 ch = @reader.peek 696 if (ch == @@kJSONObjectEnd) 697 field_type = Types::STOP 698 else 699 field_id = read_json_integer 700 read_json_object_start 701 field_type = get_type_id_for_type_name(read_json_string) 702 end 703 [nil, field_type, field_id] 704 end
Source
# File lib/thrift/protocol/json_protocol.rb 706 def read_field_end 707 read_json_object_end 708 end
Source
# File lib/thrift/protocol/json_protocol.rb 758 def read_i16 759 read_json_integer 760 end
Source
# File lib/thrift/protocol/json_protocol.rb 762 def read_i32 763 read_json_integer 764 end
Source
# File lib/thrift/protocol/json_protocol.rb 766 def read_i64 767 read_json_integer 768 end
Source
# File lib/thrift/protocol/json_protocol.rb 660 def read_json_array_end 661 read_json_syntax_char(@@kJSONArrayEnd) 662 pop_context 663 nil 664 end
Source
# File lib/thrift/protocol/json_protocol.rb 653 def read_json_array_start 654 @context.read(@reader) 655 read_json_syntax_char(@@kJSONArrayStart) 656 push_context(JSONListContext.new) 657 nil 658 end
Source
# File lib/thrift/protocol/json_protocol.rb 547 def read_json_base64 548 str = read_json_string 549 m = str.length % 4 550 if m != 0 551 # Add missing padding 552 (4 - m).times do 553 str += "=" 554 end 555 end 556 begin 557 str.unpack1("m0") 558 rescue ArgumentError 559 raise ProtocolException.new(ProtocolException::INVALID_DATA, "Invalid Base64 data") 560 end 561 end
Reads a block of base64 characters, decoding it, and returns via str
Source
# File lib/thrift/protocol/json_protocol.rb 602 def read_json_double 603 @context.read(@reader) 604 num = 0 605 if (@reader.peek == @@kJSONStringDelimiter) 606 str = read_json_string(true) 607 # Check for NaN, Infinity and -Infinity 608 if (str == @@kThriftNan) 609 num = (+1.0 / 0.0) / (+1.0 / 0.0) 610 elsif (str == @@kThriftInfinity) 611 num = +1.0 / 0.0 612 elsif (str == @@kThriftNegativeInfinity) 613 num = -1.0 / 0.0 614 else 615 if !@context.escapeNum 616 # Raise exception -- we should not be in a string in this case 617 raise ProtocolException.new(ProtocolException::INVALID_DATA, "Numeric data unexpectedly quoted") 618 end 619 begin 620 num = Float(str) 621 rescue 622 raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected numeric value; got \"#{str}\"") 623 end 624 end 625 else 626 if @context.escapeNum 627 # This will throw - we should have had a quote if escapeNum == true 628 read_json_syntax_char(@@kJSONStringDelimiter) 629 end 630 str = read_json_numeric_chars 631 begin 632 num = Float(str) 633 rescue 634 raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected numeric value; got \"#{str}\"") 635 end 636 end 637 num 638 end
Reads a JSON number or string and interprets it as a double.
Source
# File lib/thrift/protocol/json_protocol.rb 485 def read_json_escape_char 486 code_unit = read_unicode_code_unit 487 488 if code_unit.between?(0xD800, 0xDBFF) 489 slash = @reader.read 490 marker = @reader.read 491 invalid_unicode!("Unpaired UTF-16 high surrogate") unless slash == "\\" && marker == "u" 492 493 low_surrogate = read_unicode_code_unit 494 invalid_unicode!("Unpaired UTF-16 high surrogate") unless low_surrogate.between?(0xDC00, 0xDFFF) 495 codepoint = 0x10000 + ((code_unit - 0xD800) << 10) + (low_surrogate - 0xDC00) 496 [codepoint].pack("U") 497 elsif code_unit.between?(0xDC00, 0xDFFF) 498 invalid_unicode!("Unpaired UTF-16 low surrogate") 499 else 500 [code_unit].pack("U") 501 end 502 rescue EOFError 503 invalid_unicode!("Incomplete Unicode escape") 504 end
Source
# File lib/thrift/protocol/json_protocol.rb 581 def read_json_integer 582 @context.read(@reader) 583 if @context.escapeNum 584 read_json_syntax_char(@@kJSONStringDelimiter) 585 end 586 str = read_json_numeric_chars 587 588 begin 589 num = Integer(str); 590 rescue 591 raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected numeric value; got \"#{str}\"") 592 end 593 594 if @context.escapeNum 595 read_json_syntax_char(@@kJSONStringDelimiter) 596 end 597 598 num 599 end
Reads a sequence of characters and assembles them into a number, returning them via num
Source
# File lib/thrift/protocol/json_protocol.rb 565 def read_json_numeric_chars 566 str = String.new(encoding: Encoding::UTF_8) 567 while true 568 ch = @reader.peek 569 if !is_json_numeric(ch) 570 break; 571 end 572 ch = @reader.read 573 str << ch 574 check_string_size(str.bytesize) 575 end 576 str 577 end
Reads a sequence of characters, stopping at the first one that is not a valid JSON numeric character.
Source
# File lib/thrift/protocol/json_protocol.rb 647 def read_json_object_end 648 read_json_syntax_char(@@kJSONObjectEnd) 649 pop_context 650 nil 651 end
Source
# File lib/thrift/protocol/json_protocol.rb 640 def read_json_object_start 641 @context.read(@reader) 642 read_json_syntax_char(@@kJSONObjectStart) 643 push_context(JSONPairContext.new) 644 nil 645 end
Source
# File lib/thrift/protocol/json_protocol.rb 507 def read_json_string(skipContext = false) 508 # This string's characters must match up with the elements in escape_char_vals. 509 # JSON permits the optional solidus escape "\/"; see RFC 8259, section 7. 510 escape_chars = "\"\\/bfnrt" 511 512 # The elements of this array must match up with the sequence of characters in 513 # escape_chars 514 escape_char_vals = [ 515 "\"", "\\", "/", "\b", "\f", "\n", "\r", "\t", 516 ] 517 518 if !skipContext 519 @context.read(@reader) 520 end 521 read_json_syntax_char(@@kJSONStringDelimiter) 522 str = Bytes.empty_byte_buffer 523 while true 524 ch = @reader.read 525 if (ch == @@kJSONStringDelimiter) 526 break 527 end 528 if (ch == @@kJSONBackslash) 529 ch = @reader.read 530 if (ch == "u") 531 ch = read_json_escape_char 532 else 533 pos = escape_chars.index(ch); 534 if pos.nil? # not found 535 raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected control char, got '#{ch}'.") 536 end 537 ch = escape_char_vals[pos] 538 end 539 end 540 str << Bytes.force_binary_encoding(ch) 541 check_string_size(str.bytesize) 542 end 543 decode_json_string(str) 544 end
Decodes a JSON string, including unescaping, and returns the string via str
Source
# File lib/thrift/protocol/json_protocol.rb 481 def read_json_syntax_char(ch) 482 JsonProtocol::read_syntax_char(@reader, ch) 483 end
Reads 1 byte and verifies that it matches ch.
Source
# File lib/thrift/protocol/json_protocol.rb 725 def read_list_begin 726 read_json_array_start 727 type = get_type_id_for_type_name(read_json_string) 728 size = read_json_integer 729 validate_container_size(size) 730 [type, size] 731 end
Source
# File lib/thrift/protocol/json_protocol.rb 733 def read_list_end 734 read_json_array_end 735 end
Source
# File lib/thrift/protocol/json_protocol.rb 710 def read_map_begin 711 read_json_array_start 712 key_type = get_type_id_for_type_name(read_json_string) 713 val_type = get_type_id_for_type_name(read_json_string) 714 size = read_json_integer 715 validate_container_size(size) 716 read_json_object_start 717 [key_type, val_type, size] 718 end
Source
# File lib/thrift/protocol/json_protocol.rb 720 def read_map_end 721 read_json_object_end 722 read_json_array_end 723 end
Source
# File lib/thrift/protocol/json_protocol.rb 666 def read_message_begin 667 read_json_array_start 668 version = read_json_integer 669 if (version != @@kThriftVersion1) 670 raise ProtocolException.new(ProtocolException::BAD_VERSION, "Message contained bad version.") 671 end 672 name = read_json_string 673 message_type = read_json_integer 674 seqid = read_json_integer 675 [name, message_type, seqid] 676 end
Source
# File lib/thrift/protocol/json_protocol.rb 678 def read_message_end 679 read_json_array_end 680 nil 681 end
Source
# File lib/thrift/protocol/json_protocol.rb 737 def read_set_begin 738 read_json_array_start 739 type = get_type_id_for_type_name(read_json_string) 740 size = read_json_integer 741 validate_container_size(size) 742 [type, size] 743 end
Source
# File lib/thrift/protocol/json_protocol.rb 745 def read_set_end 746 read_json_array_end 747 end
Source
# File lib/thrift/protocol/json_protocol.rb 774 def read_string 775 read_json_string 776 end
Source
# File lib/thrift/protocol/json_protocol.rb 683 def read_struct_begin 684 read_json_object_start 685 nil 686 end
Source
# File lib/thrift/protocol/json_protocol.rb 688 def read_struct_end 689 read_json_object_end 690 nil 691 end
Source
# File lib/thrift/protocol/json_protocol.rb 786 def read_uuid 787 uuid = read_json_string 788 UUID.validate_uuid!(uuid) 789 uuid.tap(&:downcase!) 790 end
Source
# File lib/thrift/protocol/json_protocol.rb 782 def skip_string 783 read_string 784 end
Source
# File lib/thrift/protocol/json_protocol.rb 792 def to_s 793 "json(#{super})" 794 end
Source
# File lib/thrift/protocol/json_protocol.rb 467 def write_binary(str) 468 write_json_base64(str) 469 end
Source
# File lib/thrift/protocol/json_protocol.rb 439 def write_bool(bool) 440 write_json_integer(bool ? 1 : 0) 441 end
Source
# File lib/thrift/protocol/json_protocol.rb 443 def write_byte(byte) 444 write_json_integer(byte) 445 end
Source
# File lib/thrift/protocol/json_protocol.rb 459 def write_double(dub) 460 write_json_double(dub) 461 end
Source
# File lib/thrift/protocol/json_protocol.rb 394 def write_field_begin(name, type, id) 395 write_json_integer(id) 396 write_json_object_start 397 write_json_string(get_type_name_for_type_id(type)) 398 end
Source
# File lib/thrift/protocol/json_protocol.rb 400 def write_field_end 401 write_json_object_end 402 end
Source
# File lib/thrift/protocol/json_protocol.rb 404 def write_field_stop; nil; end
Source
# File lib/thrift/protocol/json_protocol.rb 447 def write_i16(i16) 448 write_json_integer(i16) 449 end
Source
# File lib/thrift/protocol/json_protocol.rb 451 def write_i32(i32) 452 write_json_integer(i32) 453 end
Source
# File lib/thrift/protocol/json_protocol.rb 455 def write_i64(i64) 456 write_json_integer(i64) 457 end
Source
# File lib/thrift/protocol/json_protocol.rb 369 def write_json_array_end 370 pop_context 371 trans.write(@@kJSONArrayEnd) 372 end
Source
# File lib/thrift/protocol/json_protocol.rb 363 def write_json_array_start 364 @context.write(trans) 365 trans.write(@@kJSONArrayStart) 366 push_context(JSONListContext.new); 367 end
Source
# File lib/thrift/protocol/json_protocol.rb 302 def write_json_base64(str) 303 @context.write(trans) 304 trans.write(@@kJSONStringDelimiter) 305 trans.write([str].pack("m0")) 306 trans.write(@@kJSONStringDelimiter) 307 end
Write out the contents of the string as JSON string, base64-encoding the string’s contents, and escaping as appropriate
Source
# File lib/thrift/protocol/json_protocol.rb 252 def write_json_char(ch) 253 # This table describes the handling for the first 0x30 characters 254 # 0 : escape using "\u00xx" notation 255 # 1 : just output index 256 # <other> : escape using "\<other>" notation 257 kJSONCharTable = [ 258 # 0 1 2 3 4 5 6 7 8 9 A B C D E F 259 0, 0, 0, 0, 0, 0, 0, 0, "b", "t", "n", 0, "f", "r", 0, 0, # 0 260 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 1 261 1, 1, '"', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, # 2 262 ] 263 264 ch_value = ch[0] 265 if (ch_value.kind_of? String) 266 ch_value = ch.bytes.first 267 end 268 if (ch_value >= 0x30) 269 if (ch == @@kJSONBackslash) # Only special character >= 0x30 is '\' 270 trans.write(@@kJSONBackslash) 271 trans.write(@@kJSONBackslash) 272 else 273 trans.write(ch) 274 end 275 else 276 outCh = kJSONCharTable[ch_value]; 277 # Check if regular character, backslash escaped, or JSON escaped 278 if outCh.kind_of? String 279 trans.write(@@kJSONBackslash) 280 trans.write(outCh) 281 elsif outCh == 1 282 trans.write(ch) 283 else 284 write_json_escape_char(ch) 285 end 286 end 287 end
Write the character ch as part of a JSON string, escaping as appropriate.
Source
# File lib/thrift/protocol/json_protocol.rb 325 def write_json_double(num) 326 @context.write(trans) 327 # Normalize output of thrift::to_string for NaNs and Infinities 328 special = false; 329 if num.nan? 330 special = true; 331 val = @@kThriftNan; 332 elsif num.infinite? 333 special = true; 334 val = @@kThriftInfinity; 335 if (num < 0.0) 336 val = @@kThriftNegativeInfinity; 337 end 338 else 339 val = num.to_s 340 end 341 342 escapeNum = special || @context.escapeNum 343 if escapeNum 344 trans.write(@@kJSONStringDelimiter) 345 end 346 trans.write(val) 347 if escapeNum 348 trans.write(@@kJSONStringDelimiter) 349 end 350 end
Convert the given double to a JSON string, which is either the number, “NaN” or “Infinity” or “-Infinity”.
Source
# File lib/thrift/protocol/json_protocol.rb 242 def write_json_escape_char(ch) 243 trans.write('\\u') 244 ch_value = ch[0] 245 if (ch_value.kind_of? String) 246 ch_value = ch.bytes.first 247 end 248 trans.write(ch_value.to_s(16).rjust(4, "0")) 249 end
Write the character ch as a JSON escape sequence (“u00xx”)
Source
# File lib/thrift/protocol/json_protocol.rb 311 def write_json_integer(num) 312 @context.write(trans) 313 escapeNum = @context.escapeNum 314 if escapeNum 315 trans.write(@@kJSONStringDelimiter) 316 end 317 trans.write(num.to_s); 318 if escapeNum 319 trans.write(@@kJSONStringDelimiter) 320 end 321 end
Convert the given integer type to a JSON number, or a string if the context requires it (eg: key in a map pair).
Source
# File lib/thrift/protocol/json_protocol.rb 358 def write_json_object_end 359 pop_context 360 trans.write(@@kJSONObjectEnd) 361 end
Source
# File lib/thrift/protocol/json_protocol.rb 352 def write_json_object_start 353 @context.write(trans) 354 trans.write(@@kJSONObjectStart) 355 push_context(JSONPairContext.new); 356 end
Source
# File lib/thrift/protocol/json_protocol.rb 290 def write_json_string(str) 291 str = normalize_json_string(str) 292 @context.write(trans) 293 trans.write(@@kJSONStringDelimiter) 294 str.each_char do |ch| 295 write_json_char(ch) 296 end 297 trans.write(@@kJSONStringDelimiter) 298 end
Write out the contents of the string str as a JSON string, escaping characters as appropriate.
Source
# File lib/thrift/protocol/json_protocol.rb 419 def write_list_begin(etype, size) 420 write_json_array_start 421 write_json_string(get_type_name_for_type_id(etype)) 422 write_json_integer(size) 423 end
Source
# File lib/thrift/protocol/json_protocol.rb 425 def write_list_end 426 write_json_array_end 427 end
Source
# File lib/thrift/protocol/json_protocol.rb 406 def write_map_begin(ktype, vtype, size) 407 write_json_array_start 408 write_json_string(get_type_name_for_type_id(ktype)) 409 write_json_string(get_type_name_for_type_id(vtype)) 410 write_json_integer(size) 411 write_json_object_start 412 end
Source
# File lib/thrift/protocol/json_protocol.rb 414 def write_map_end 415 write_json_object_end 416 write_json_array_end 417 end
Source
# File lib/thrift/protocol/json_protocol.rb 374 def write_message_begin(name, type, seqid) 375 write_json_array_start 376 write_json_integer(@@kThriftVersion1) 377 write_json_string(name) 378 write_json_integer(type) 379 write_json_integer(seqid) 380 end
Source
# File lib/thrift/protocol/json_protocol.rb 382 def write_message_end 383 write_json_array_end 384 end
Source
# File lib/thrift/protocol/json_protocol.rb 429 def write_set_begin(etype, size) 430 write_json_array_start 431 write_json_string(get_type_name_for_type_id(etype)) 432 write_json_integer(size) 433 end
Source
# File lib/thrift/protocol/json_protocol.rb 435 def write_set_end 436 write_json_array_end 437 end
Source
# File lib/thrift/protocol/json_protocol.rb 463 def write_string(str) 464 write_json_string(str) 465 end
Source
# File lib/thrift/protocol/json_protocol.rb 386 def write_struct_begin(name) 387 write_json_object_start 388 end
Source
# File lib/thrift/protocol/json_protocol.rb 390 def write_struct_end 391 write_json_object_end 392 end
Source
# File lib/thrift/protocol/json_protocol.rb 471 def write_uuid(uuid) 472 UUID.validate_uuid!(uuid) 473 write_json_string(uuid.downcase) 474 end
Private Instance Methods
Source
# File lib/thrift/protocol/json_protocol.rb 812 def decode_json_string(bytes) 813 bytes.force_encoding(Encoding::UTF_8) 814 invalid_unicode!("Invalid UTF-8 string") unless bytes.valid_encoding? 815 bytes 816 end
Source
# File lib/thrift/protocol/json_protocol.rb 827 def invalid_unicode!(message) 828 raise ProtocolException.new(ProtocolException::INVALID_DATA, message) 829 end
Source
# File lib/thrift/protocol/json_protocol.rb 798 def normalize_json_string(str) 799 utf8 = if str.encoding == Encoding::UTF_8 800 str 801 elsif str.encoding == Encoding::BINARY 802 str.ascii_only? ? str : str.dup.force_encoding(Encoding::UTF_8) 803 else 804 str.encode(Encoding::UTF_8) 805 end 806 invalid_unicode!("Invalid UTF-8 string") unless utf8.valid_encoding? 807 utf8 808 rescue EncodingError 809 invalid_unicode!("Invalid UTF-8 string") 810 end
Source
# File lib/thrift/protocol/json_protocol.rb 818 def read_unicode_code_unit 819 hex = Bytes.empty_byte_buffer 820 4.times { hex << @reader.read } 821 invalid_unicode!("Invalid Unicode escape") unless hex.match?(/\A[0-9a-fA-F]{4}\z/) 822 hex.to_i(16) 823 rescue EOFError 824 invalid_unicode!("Incomplete Unicode escape") 825 end