Class | Thrift::JsonProtocol |
In: |
lib/thrift/protocol/json_protocol.rb
lib/thrift/protocol/json_protocol.rb |
Parent: | BaseProtocol |
# File lib/thrift/protocol/json_protocol.rb, line 149 149: def initialize(trans) 150: super(trans) 151: @context = JSONContext.new 152: @contexts = Array.new 153: @reader = LookaheadReader.new(trans) 154: end
# File lib/thrift/protocol/json_protocol.rb, line 149 149: def initialize(trans) 150: super(trans) 151: @context = JSONContext.new 152: @contexts = Array.new 153: @reader = LookaheadReader.new(trans) 154: end
Read 1 character from the trans and verify that it is the expected character ch. Throw a protocol exception if it is not.
# File lib/thrift/protocol/json_protocol.rb, line 221 221: def self.read_syntax_char(reader, ch) 222: ch2 = reader.read 223: if (ch2 != ch) 224: raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected \'#{ch}\' got \'#{ch2}\'.") 225: end 226: end
Read 1 character from the trans and verify that it is the expected character ch. Throw a protocol exception if it is not.
# File lib/thrift/protocol/json_protocol.rb, line 221 221: def self.read_syntax_char(reader, ch) 222: ch2 = reader.read 223: if (ch2 != ch) 224: raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected \'#{ch}\' got \'#{ch2}\'.") 225: end 226: end
# File lib/thrift/protocol/json_protocol.rb, line 185 185: def get_type_id_for_type_name(name) 186: if (name == "tf") 187: result = Types::BOOL 188: elsif (name == "i8") 189: result = Types::BYTE 190: elsif (name == "i16") 191: result = Types::I16 192: elsif (name == "i32") 193: result = Types::I32 194: elsif (name == "i64") 195: result = Types::I64 196: elsif (name == "dbl") 197: result = Types::DOUBLE 198: elsif (name == "str") 199: result = Types::STRING 200: elsif (name == "rec") 201: result = Types::STRUCT 202: elsif (name == "map") 203: result = Types::MAP 204: elsif (name == "set") 205: result = Types::SET 206: elsif (name == "lst") 207: result = Types::LIST 208: else 209: result = Types::STOP 210: end 211: if (result == Types::STOP) 212: raise NotImplementedError 213: end 214: return result 215: end
# File lib/thrift/protocol/json_protocol.rb, line 185 185: def get_type_id_for_type_name(name) 186: if (name == "tf") 187: result = Types::BOOL 188: elsif (name == "i8") 189: result = Types::BYTE 190: elsif (name == "i16") 191: result = Types::I16 192: elsif (name == "i32") 193: result = Types::I32 194: elsif (name == "i64") 195: result = Types::I64 196: elsif (name == "dbl") 197: result = Types::DOUBLE 198: elsif (name == "str") 199: result = Types::STRING 200: elsif (name == "rec") 201: result = Types::STRUCT 202: elsif (name == "map") 203: result = Types::MAP 204: elsif (name == "set") 205: result = Types::SET 206: elsif (name == "lst") 207: result = Types::LIST 208: else 209: result = Types::STOP 210: end 211: if (result == Types::STOP) 212: raise NotImplementedError 213: end 214: return result 215: end
# File lib/thrift/protocol/json_protocol.rb, line 156 156: def get_type_name_for_type_id(id) 157: case id 158: when Types::BOOL 159: "tf" 160: when Types::BYTE 161: "i8" 162: when Types::I16 163: "i16" 164: when Types::I32 165: "i32" 166: when Types::I64 167: "i64" 168: when Types::DOUBLE 169: "dbl" 170: when Types::STRING 171: "str" 172: when Types::STRUCT 173: "rec" 174: when Types::MAP 175: "map" 176: when Types::SET 177: "set" 178: when Types::LIST 179: "lst" 180: else 181: raise NotImplementedError 182: end 183: end
# File lib/thrift/protocol/json_protocol.rb, line 156 156: def get_type_name_for_type_id(id) 157: case id 158: when Types::BOOL 159: "tf" 160: when Types::BYTE 161: "i8" 162: when Types::I16 163: "i16" 164: when Types::I32 165: "i32" 166: when Types::I64 167: "i64" 168: when Types::DOUBLE 169: "dbl" 170: when Types::STRING 171: "str" 172: when Types::STRUCT 173: "rec" 174: when Types::MAP 175: "map" 176: when Types::SET 177: "set" 178: when Types::LIST 179: "lst" 180: else 181: raise NotImplementedError 182: end 183: end
Return true if the character ch is in [-+0-9.Ee]; false otherwise
# File lib/thrift/protocol/json_protocol.rb, line 229 229: def is_json_numeric(ch) 230: case ch 231: when '+', '-', '.', '0' .. '9', 'E', "e" 232: return true 233: else 234: return false 235: end 236: end
Return true if the character ch is in [-+0-9.Ee]; false otherwise
# File lib/thrift/protocol/json_protocol.rb, line 229 229: def is_json_numeric(ch) 230: case ch 231: when '+', '-', '.', '0' .. '9', 'E', "e" 232: return true 233: else 234: return false 235: end 236: end
# File lib/thrift/protocol/json_protocol.rb, line 243 243: def pop_context 244: @context = @contexts.pop 245: end
# File lib/thrift/protocol/json_protocol.rb, line 243 243: def pop_context 244: @context = @contexts.pop 245: end
# File lib/thrift/protocol/json_protocol.rb, line 238 238: def push_context(context) 239: @contexts.push(@context) 240: @context = context 241: end
# File lib/thrift/protocol/json_protocol.rb, line 238 238: def push_context(context) 239: @contexts.push(@context) 240: @context = context 241: end
# File lib/thrift/protocol/json_protocol.rb, line 755 755: def read_binary 756: read_json_base64 757: end
# File lib/thrift/protocol/json_protocol.rb, line 755 755: def read_binary 756: read_json_base64 757: end
# File lib/thrift/protocol/json_protocol.rb, line 726 726: def read_bool 727: byte = read_byte 728: byte != 0 729: end
# File lib/thrift/protocol/json_protocol.rb, line 726 726: def read_bool 727: byte = read_byte 728: byte != 0 729: end
# File lib/thrift/protocol/json_protocol.rb, line 731 731: def read_byte 732: read_json_integer 733: end
# File lib/thrift/protocol/json_protocol.rb, line 731 731: def read_byte 732: read_json_integer 733: end
# File lib/thrift/protocol/json_protocol.rb, line 747 747: def read_double 748: read_json_double 749: end
# File lib/thrift/protocol/json_protocol.rb, line 747 747: def read_double 748: read_json_double 749: end
# File lib/thrift/protocol/json_protocol.rb, line 678 678: def read_field_begin 679: # Check if we hit the end of the list 680: ch = @reader.peek 681: if (ch == @@kJSONObjectEnd) 682: field_type = Types::STOP 683: else 684: field_id = read_json_integer 685: read_json_object_start 686: field_type = get_type_id_for_type_name(read_json_string) 687: end 688: [nil, field_type, field_id] 689: end
# File lib/thrift/protocol/json_protocol.rb, line 678 678: def read_field_begin 679: # Check if we hit the end of the list 680: ch = @reader.peek 681: if (ch == @@kJSONObjectEnd) 682: field_type = Types::STOP 683: else 684: field_id = read_json_integer 685: read_json_object_start 686: field_type = get_type_id_for_type_name(read_json_string) 687: end 688: [nil, field_type, field_id] 689: end
# File lib/thrift/protocol/json_protocol.rb, line 691 691: def read_field_end 692: read_json_object_end 693: end
# File lib/thrift/protocol/json_protocol.rb, line 691 691: def read_field_end 692: read_json_object_end 693: end
# File lib/thrift/protocol/json_protocol.rb, line 735 735: def read_i16 736: read_json_integer 737: end
# File lib/thrift/protocol/json_protocol.rb, line 735 735: def read_i16 736: read_json_integer 737: end
# File lib/thrift/protocol/json_protocol.rb, line 739 739: def read_i32 740: read_json_integer 741: end
# File lib/thrift/protocol/json_protocol.rb, line 739 739: def read_i32 740: read_json_integer 741: end
# File lib/thrift/protocol/json_protocol.rb, line 743 743: def read_i64 744: read_json_integer 745: end
# File lib/thrift/protocol/json_protocol.rb, line 743 743: def read_i64 744: read_json_integer 745: end
# File lib/thrift/protocol/json_protocol.rb, line 645 645: def read_json_array_end 646: read_json_syntax_char(@@kJSONArrayEnd) 647: pop_context 648: nil 649: end
# File lib/thrift/protocol/json_protocol.rb, line 645 645: def read_json_array_end 646: read_json_syntax_char(@@kJSONArrayEnd) 647: pop_context 648: nil 649: end
# File lib/thrift/protocol/json_protocol.rb, line 638 638: def read_json_array_start 639: @context.read(@reader) 640: read_json_syntax_char(@@kJSONArrayStart) 641: push_context(JSONListContext.new) 642: nil 643: end
# File lib/thrift/protocol/json_protocol.rb, line 638 638: def read_json_array_start 639: @context.read(@reader) 640: read_json_syntax_char(@@kJSONArrayStart) 641: push_context(JSONListContext.new) 642: nil 643: end
Reads a block of base64 characters, decoding it, and returns via str
# File lib/thrift/protocol/json_protocol.rb, line 545 545: def read_json_base64 546: read_json_string.unpack("m")[0] 547: end
Reads a block of base64 characters, decoding it, and returns via str
# File lib/thrift/protocol/json_protocol.rb, line 545 545: def read_json_base64 546: read_json_string.unpack("m")[0] 547: end
Reads a JSON number or string and interprets it as a double.
# File lib/thrift/protocol/json_protocol.rb, line 587 587: def read_json_double 588: @context.read(@reader) 589: num = 0 590: if (@reader.peek == @@kJSONStringDelimiter) 591: str = read_json_string(true) 592: # Check for NaN, Infinity and -Infinity 593: if (str == @@kThriftNan) 594: num = (+1.0/0.0)/(+1.0/0.0) 595: elsif (str == @@kThriftInfinity) 596: num = +1.0/0.0 597: elsif (str == @@kThriftNegativeInfinity) 598: num = -1.0/0.0 599: else 600: if (!@context.escapeNum) 601: # Raise exception -- we should not be in a string in this case 602: raise ProtocolException.new(ProtocolException::INVALID_DATA, "Numeric data unexpectedly quoted") 603: end 604: begin 605: num = Float(str) 606: rescue 607: raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected numeric value; got \"#{str}\"") 608: end 609: end 610: else 611: if (@context.escapeNum) 612: # This will throw - we should have had a quote if escapeNum == true 613: read_json_syntax_char(@@kJSONStringDelimiter) 614: end 615: str = read_json_numeric_chars 616: begin 617: num = Float(str) 618: rescue 619: raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected numeric value; got \"#{str}\"") 620: end 621: end 622: return num 623: end
Reads a JSON number or string and interprets it as a double.
# File lib/thrift/protocol/json_protocol.rb, line 587 587: def read_json_double 588: @context.read(@reader) 589: num = 0 590: if (@reader.peek == @@kJSONStringDelimiter) 591: str = read_json_string(true) 592: # Check for NaN, Infinity and -Infinity 593: if (str == @@kThriftNan) 594: num = (+1.0/0.0)/(+1.0/0.0) 595: elsif (str == @@kThriftInfinity) 596: num = +1.0/0.0 597: elsif (str == @@kThriftNegativeInfinity) 598: num = -1.0/0.0 599: else 600: if (!@context.escapeNum) 601: # Raise exception -- we should not be in a string in this case 602: raise ProtocolException.new(ProtocolException::INVALID_DATA, "Numeric data unexpectedly quoted") 603: end 604: begin 605: num = Float(str) 606: rescue 607: raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected numeric value; got \"#{str}\"") 608: end 609: end 610: else 611: if (@context.escapeNum) 612: # This will throw - we should have had a quote if escapeNum == true 613: read_json_syntax_char(@@kJSONStringDelimiter) 614: end 615: str = read_json_numeric_chars 616: begin 617: num = Float(str) 618: rescue 619: raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected numeric value; got \"#{str}\"") 620: end 621: end 622: return num 623: end
Decodes the four hex parts of a JSON escaped string character and returns the character via out.
Note - this only supports Unicode characters in the BMP (U+0000 to U+FFFF); characters above the BMP are encoded as two escape sequences (surrogate pairs), which is not yet implemented
# File lib/thrift/protocol/json_protocol.rb, line 491 491: def read_json_escape_char 492: str = @reader.read 493: str += @reader.read 494: str += @reader.read 495: str += @reader.read 496: if RUBY_VERSION >= '1.9' 497: str.hex.chr(Encoding::UTF_8) 498: else 499: str.hex.chr 500: end 501: end
Decodes the four hex parts of a JSON escaped string character and returns the character via out.
Note - this only supports Unicode characters in the BMP (U+0000 to U+FFFF); characters above the BMP are encoded as two escape sequences (surrogate pairs), which is not yet implemented
# File lib/thrift/protocol/json_protocol.rb, line 491 491: def read_json_escape_char 492: str = @reader.read 493: str += @reader.read 494: str += @reader.read 495: str += @reader.read 496: if RUBY_VERSION >= '1.9' 497: str.hex.chr(Encoding::UTF_8) 498: else 499: str.hex.chr 500: end 501: end
Reads a sequence of characters and assembles them into a number, returning them via num
# File lib/thrift/protocol/json_protocol.rb, line 566 566: def read_json_integer 567: @context.read(@reader) 568: if (@context.escapeNum) 569: read_json_syntax_char(@@kJSONStringDelimiter) 570: end 571: str = read_json_numeric_chars 572: 573: begin 574: num = Integer(str); 575: rescue 576: raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected numeric value; got \"#{str}\"") 577: end 578: 579: if (@context.escapeNum) 580: read_json_syntax_char(@@kJSONStringDelimiter) 581: end 582: 583: return num 584: end
Reads a sequence of characters and assembles them into a number, returning them via num
# File lib/thrift/protocol/json_protocol.rb, line 566 566: def read_json_integer 567: @context.read(@reader) 568: if (@context.escapeNum) 569: read_json_syntax_char(@@kJSONStringDelimiter) 570: end 571: str = read_json_numeric_chars 572: 573: begin 574: num = Integer(str); 575: rescue 576: raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected numeric value; got \"#{str}\"") 577: end 578: 579: if (@context.escapeNum) 580: read_json_syntax_char(@@kJSONStringDelimiter) 581: end 582: 583: return num 584: end
Reads a sequence of characters, stopping at the first one that is not a valid JSON numeric character.
# File lib/thrift/protocol/json_protocol.rb, line 551 551: def read_json_numeric_chars 552: str = "" 553: while (true) 554: ch = @reader.peek 555: if (!is_json_numeric(ch)) 556: break; 557: end 558: ch = @reader.read 559: str += ch 560: end 561: return str 562: end
Reads a sequence of characters, stopping at the first one that is not a valid JSON numeric character.
# File lib/thrift/protocol/json_protocol.rb, line 551 551: def read_json_numeric_chars 552: str = "" 553: while (true) 554: ch = @reader.peek 555: if (!is_json_numeric(ch)) 556: break; 557: end 558: ch = @reader.read 559: str += ch 560: end 561: return str 562: end
# File lib/thrift/protocol/json_protocol.rb, line 632 632: def read_json_object_end 633: read_json_syntax_char(@@kJSONObjectEnd) 634: pop_context 635: nil 636: end
# File lib/thrift/protocol/json_protocol.rb, line 632 632: def read_json_object_end 633: read_json_syntax_char(@@kJSONObjectEnd) 634: pop_context 635: nil 636: end
# File lib/thrift/protocol/json_protocol.rb, line 625 625: def read_json_object_start 626: @context.read(@reader) 627: read_json_syntax_char(@@kJSONObjectStart) 628: push_context(JSONPairContext.new) 629: nil 630: end
# File lib/thrift/protocol/json_protocol.rb, line 625 625: def read_json_object_start 626: @context.read(@reader) 627: read_json_syntax_char(@@kJSONObjectStart) 628: push_context(JSONPairContext.new) 629: nil 630: end
Decodes a JSON string, including unescaping, and returns the string via str
# File lib/thrift/protocol/json_protocol.rb, line 504 504: def read_json_string(skipContext = false) 505: # This string's characters must match up with the elements in escape_char_vals. 506: # I don't have '/' on this list even though it appears on www.json.org -- 507: # it is not in the RFC 508: escape_chars = "\"\\bfnrt" 509: 510: # The elements of this array must match up with the sequence of characters in 511: # escape_chars 512: escape_char_vals = [ 513: '"', '\\', '\b', '\f', '\n', '\r', '\t', 514: ] 515: 516: if !skipContext 517: @context.read(@reader) 518: end 519: read_json_syntax_char(@@kJSONStringDelimiter) 520: ch = "" 521: str = "" 522: while (true) 523: ch = @reader.read 524: if (ch == @@kJSONStringDelimiter) 525: break 526: end 527: if (ch == @@kJSONBackslash) 528: ch = @reader.read 529: if (ch == 'u') 530: ch = read_json_escape_char 531: else 532: pos = escape_chars.index(ch); 533: if (pos.nil?) # not found 534: raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected control char, got \'#{ch}\'.") 535: end 536: ch = escape_char_vals[pos] 537: end 538: end 539: str += ch 540: end 541: return str 542: end
Decodes a JSON string, including unescaping, and returns the string via str
# File lib/thrift/protocol/json_protocol.rb, line 504 504: def read_json_string(skipContext = false) 505: # This string's characters must match up with the elements in escape_char_vals. 506: # I don't have '/' on this list even though it appears on www.json.org -- 507: # it is not in the RFC 508: escape_chars = "\"\\bfnrt" 509: 510: # The elements of this array must match up with the sequence of characters in 511: # escape_chars 512: escape_char_vals = [ 513: '"', '\\', '\b', '\f', '\n', '\r', '\t', 514: ] 515: 516: if !skipContext 517: @context.read(@reader) 518: end 519: read_json_syntax_char(@@kJSONStringDelimiter) 520: ch = "" 521: str = "" 522: while (true) 523: ch = @reader.read 524: if (ch == @@kJSONStringDelimiter) 525: break 526: end 527: if (ch == @@kJSONBackslash) 528: ch = @reader.read 529: if (ch == 'u') 530: ch = read_json_escape_char 531: else 532: pos = escape_chars.index(ch); 533: if (pos.nil?) # not found 534: raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected control char, got \'#{ch}\'.") 535: end 536: ch = escape_char_vals[pos] 537: end 538: end 539: str += ch 540: end 541: return str 542: end
Reads 1 byte and verifies that it matches ch.
# File lib/thrift/protocol/json_protocol.rb, line 481 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.
# File lib/thrift/protocol/json_protocol.rb, line 481 481: def read_json_syntax_char(ch) 482: JsonProtocol::read_syntax_char(@reader, ch) 483: end
# File lib/thrift/protocol/json_protocol.rb, line 709 709: def read_list_begin 710: read_json_array_start 711: [get_type_id_for_type_name(read_json_string), read_json_integer] 712: end
# File lib/thrift/protocol/json_protocol.rb, line 709 709: def read_list_begin 710: read_json_array_start 711: [get_type_id_for_type_name(read_json_string), read_json_integer] 712: end
# File lib/thrift/protocol/json_protocol.rb, line 714 714: def read_list_end 715: read_json_array_end 716: end
# File lib/thrift/protocol/json_protocol.rb, line 714 714: def read_list_end 715: read_json_array_end 716: end
# File lib/thrift/protocol/json_protocol.rb, line 695 695: def read_map_begin 696: read_json_array_start 697: key_type = get_type_id_for_type_name(read_json_string) 698: val_type = get_type_id_for_type_name(read_json_string) 699: size = read_json_integer 700: read_json_object_start 701: [key_type, val_type, size] 702: end
# File lib/thrift/protocol/json_protocol.rb, line 695 695: def read_map_begin 696: read_json_array_start 697: key_type = get_type_id_for_type_name(read_json_string) 698: val_type = get_type_id_for_type_name(read_json_string) 699: size = read_json_integer 700: read_json_object_start 701: [key_type, val_type, size] 702: end
# File lib/thrift/protocol/json_protocol.rb, line 704 704: def read_map_end 705: read_json_object_end 706: read_json_array_end 707: end
# File lib/thrift/protocol/json_protocol.rb, line 704 704: def read_map_end 705: read_json_object_end 706: read_json_array_end 707: end
# File lib/thrift/protocol/json_protocol.rb, line 651 651: def read_message_begin 652: read_json_array_start 653: version = read_json_integer 654: if (version != @@kThriftVersion1) 655: raise ProtocolException.new(ProtocolException::BAD_VERSION, 'Message contained bad version.') 656: end 657: name = read_json_string 658: message_type = read_json_integer 659: seqid = read_json_integer 660: [name, message_type, seqid] 661: end
# File lib/thrift/protocol/json_protocol.rb, line 651 651: def read_message_begin 652: read_json_array_start 653: version = read_json_integer 654: if (version != @@kThriftVersion1) 655: raise ProtocolException.new(ProtocolException::BAD_VERSION, 'Message contained bad version.') 656: end 657: name = read_json_string 658: message_type = read_json_integer 659: seqid = read_json_integer 660: [name, message_type, seqid] 661: end
# File lib/thrift/protocol/json_protocol.rb, line 663 663: def read_message_end 664: read_json_array_end 665: nil 666: end
# File lib/thrift/protocol/json_protocol.rb, line 663 663: def read_message_end 664: read_json_array_end 665: nil 666: end
# File lib/thrift/protocol/json_protocol.rb, line 718 718: def read_set_begin 719: read_json_array_start 720: end
# File lib/thrift/protocol/json_protocol.rb, line 718 718: def read_set_begin 719: read_json_array_start 720: end
# File lib/thrift/protocol/json_protocol.rb, line 722 722: def read_set_end 723: read_json_array_end 724: end
# File lib/thrift/protocol/json_protocol.rb, line 722 722: def read_set_end 723: read_json_array_end 724: end
# File lib/thrift/protocol/json_protocol.rb, line 751 751: def read_string 752: read_json_string 753: end
# File lib/thrift/protocol/json_protocol.rb, line 751 751: def read_string 752: read_json_string 753: end
# File lib/thrift/protocol/json_protocol.rb, line 668 668: def read_struct_begin 669: read_json_object_start 670: nil 671: end
# File lib/thrift/protocol/json_protocol.rb, line 668 668: def read_struct_begin 669: read_json_object_start 670: nil 671: end
# File lib/thrift/protocol/json_protocol.rb, line 673 673: def read_struct_end 674: read_json_object_end 675: nil 676: end
# File lib/thrift/protocol/json_protocol.rb, line 673 673: def read_struct_end 674: read_json_object_end 675: nil 676: end
# File lib/thrift/protocol/json_protocol.rb, line 472 472: def write_binary(str) 473: write_json_base64(str) 474: end
# File lib/thrift/protocol/json_protocol.rb, line 472 472: def write_binary(str) 473: write_json_base64(str) 474: end
# File lib/thrift/protocol/json_protocol.rb, line 444 444: def write_bool(bool) 445: write_json_integer(bool ? 1 : 0) 446: end
# File lib/thrift/protocol/json_protocol.rb, line 444 444: def write_bool(bool) 445: write_json_integer(bool ? 1 : 0) 446: end
# File lib/thrift/protocol/json_protocol.rb, line 448 448: def write_byte(byte) 449: write_json_integer(byte) 450: end
# File lib/thrift/protocol/json_protocol.rb, line 448 448: def write_byte(byte) 449: write_json_integer(byte) 450: end
# File lib/thrift/protocol/json_protocol.rb, line 464 464: def write_double(dub) 465: write_json_double(dub) 466: end
# File lib/thrift/protocol/json_protocol.rb, line 464 464: def write_double(dub) 465: write_json_double(dub) 466: end
# File lib/thrift/protocol/json_protocol.rb, line 399 399: def write_field_begin(name, type, id) 400: write_json_integer(id) 401: write_json_object_start 402: write_json_string(get_type_name_for_type_id(type)) 403: end
# File lib/thrift/protocol/json_protocol.rb, line 399 399: def write_field_begin(name, type, id) 400: write_json_integer(id) 401: write_json_object_start 402: write_json_string(get_type_name_for_type_id(type)) 403: end
# File lib/thrift/protocol/json_protocol.rb, line 405 405: def write_field_end 406: write_json_object_end 407: end
# File lib/thrift/protocol/json_protocol.rb, line 405 405: def write_field_end 406: write_json_object_end 407: end
# File lib/thrift/protocol/json_protocol.rb, line 452 452: def write_i16(i16) 453: write_json_integer(i16) 454: end
# File lib/thrift/protocol/json_protocol.rb, line 452 452: def write_i16(i16) 453: write_json_integer(i16) 454: end
# File lib/thrift/protocol/json_protocol.rb, line 456 456: def write_i32(i32) 457: write_json_integer(i32) 458: end
# File lib/thrift/protocol/json_protocol.rb, line 456 456: def write_i32(i32) 457: write_json_integer(i32) 458: end
# File lib/thrift/protocol/json_protocol.rb, line 460 460: def write_i64(i64) 461: write_json_integer(i64) 462: end
# File lib/thrift/protocol/json_protocol.rb, line 460 460: def write_i64(i64) 461: write_json_integer(i64) 462: end
# File lib/thrift/protocol/json_protocol.rb, line 374 374: def write_json_array_end 375: pop_context 376: trans.write(@@kJSONArrayEnd) 377: end
# File lib/thrift/protocol/json_protocol.rb, line 374 374: def write_json_array_end 375: pop_context 376: trans.write(@@kJSONArrayEnd) 377: end
# File lib/thrift/protocol/json_protocol.rb, line 368 368: def write_json_array_start 369: @context.write(trans) 370: trans.write(@@kJSONArrayStart) 371: push_context(JSONListContext.new); 372: end
# File lib/thrift/protocol/json_protocol.rb, line 368 368: def write_json_array_start 369: @context.write(trans) 370: trans.write(@@kJSONArrayStart) 371: push_context(JSONListContext.new); 372: end
Write out the contents of the string as JSON string, base64-encoding the string‘s contents, and escaping as appropriate
# File lib/thrift/protocol/json_protocol.rb, line 307 307: def write_json_base64(str) 308: @context.write(trans) 309: trans.write(@@kJSONStringDelimiter) 310: write_json_string([str].pack("m")) 311: trans.write(@@kJSONStringDelimiter) 312: end
Write out the contents of the string as JSON string, base64-encoding the string‘s contents, and escaping as appropriate
# File lib/thrift/protocol/json_protocol.rb, line 307 307: def write_json_base64(str) 308: @context.write(trans) 309: trans.write(@@kJSONStringDelimiter) 310: write_json_string([str].pack("m")) 311: trans.write(@@kJSONStringDelimiter) 312: end
Write the character ch as part of a JSON string, escaping as appropriate.
# File lib/thrift/protocol/json_protocol.rb, line 258 258: def write_json_char(ch) 259: # This table describes the handling for the first 0x30 characters 260: # 0 : escape using "\u00xx" notation 261: # 1 : just output index 262: # <other> : escape using "\<other>" notation 263: kJSONCharTable = [ 264: # 0 1 2 3 4 5 6 7 8 9 A B C D E F 265: 0, 0, 0, 0, 0, 0, 0, 0,'b','t','n', 0,'f','r', 0, 0, # 0 266: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 1 267: 1, 1,'"', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, # 2 268: ] 269: 270: ch_value = ch[0] 271: if (ch_value.kind_of? String) 272: ch_value = ch.bytes.first 273: end 274: if (ch_value >= 0x30) 275: if (ch == @@kJSONBackslash) # Only special character >= 0x30 is '\' 276: trans.write(@@kJSONBackslash) 277: trans.write(@@kJSONBackslash) 278: else 279: trans.write(ch) 280: end 281: else 282: outCh = kJSONCharTable[ch_value]; 283: # Check if regular character, backslash escaped, or JSON escaped 284: if outCh.kind_of? String 285: trans.write(@@kJSONBackslash) 286: trans.write(outCh) 287: elsif outCh == 1 288: trans.write(ch) 289: else 290: write_json_escape_char(ch) 291: end 292: end 293: end
Write the character ch as part of a JSON string, escaping as appropriate.
# File lib/thrift/protocol/json_protocol.rb, line 258 258: def write_json_char(ch) 259: # This table describes the handling for the first 0x30 characters 260: # 0 : escape using "\u00xx" notation 261: # 1 : just output index 262: # <other> : escape using "\<other>" notation 263: kJSONCharTable = [ 264: # 0 1 2 3 4 5 6 7 8 9 A B C D E F 265: 0, 0, 0, 0, 0, 0, 0, 0,'b','t','n', 0,'f','r', 0, 0, # 0 266: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 1 267: 1, 1,'"', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, # 2 268: ] 269: 270: ch_value = ch[0] 271: if (ch_value.kind_of? String) 272: ch_value = ch.bytes.first 273: end 274: if (ch_value >= 0x30) 275: if (ch == @@kJSONBackslash) # Only special character >= 0x30 is '\' 276: trans.write(@@kJSONBackslash) 277: trans.write(@@kJSONBackslash) 278: else 279: trans.write(ch) 280: end 281: else 282: outCh = kJSONCharTable[ch_value]; 283: # Check if regular character, backslash escaped, or JSON escaped 284: if outCh.kind_of? String 285: trans.write(@@kJSONBackslash) 286: trans.write(outCh) 287: elsif outCh == 1 288: trans.write(ch) 289: else 290: write_json_escape_char(ch) 291: end 292: end 293: end
Convert the given double to a JSON string, which is either the number, "NaN" or "Infinity" or "-Infinity".
# File lib/thrift/protocol/json_protocol.rb, line 330 330: def write_json_double(num) 331: @context.write(trans) 332: # Normalize output of boost::lexical_cast for NaNs and Infinities 333: special = false; 334: if (num.nan?) 335: special = true; 336: val = @@kThriftNan; 337: elsif (num.infinite?) 338: special = true; 339: val = @@kThriftInfinity; 340: if (num < 0.0) 341: val = @@kThriftNegativeInfinity; 342: end 343: else 344: val = num.to_s 345: end 346: 347: escapeNum = special || @context.escapeNum 348: if (escapeNum) 349: trans.write(@@kJSONStringDelimiter) 350: end 351: trans.write(val) 352: if (escapeNum) 353: trans.write(@@kJSONStringDelimiter) 354: end 355: end
Convert the given double to a JSON string, which is either the number, "NaN" or "Infinity" or "-Infinity".
# File lib/thrift/protocol/json_protocol.rb, line 330 330: def write_json_double(num) 331: @context.write(trans) 332: # Normalize output of boost::lexical_cast for NaNs and Infinities 333: special = false; 334: if (num.nan?) 335: special = true; 336: val = @@kThriftNan; 337: elsif (num.infinite?) 338: special = true; 339: val = @@kThriftInfinity; 340: if (num < 0.0) 341: val = @@kThriftNegativeInfinity; 342: end 343: else 344: val = num.to_s 345: end 346: 347: escapeNum = special || @context.escapeNum 348: if (escapeNum) 349: trans.write(@@kJSONStringDelimiter) 350: end 351: trans.write(val) 352: if (escapeNum) 353: trans.write(@@kJSONStringDelimiter) 354: end 355: end
Write the character ch as a JSON escape sequence ("\u00xx")
# File lib/thrift/protocol/json_protocol.rb, line 248 248: def write_json_escape_char(ch) 249: trans.write('\\u') 250: ch_value = ch[0] 251: if (ch_value.kind_of? String) 252: ch_value = ch.bytes.first 253: end 254: trans.write(ch_value.to_s(16).rjust(4,'0')) 255: end
Write the character ch as a JSON escape sequence ("\u00xx")
# File lib/thrift/protocol/json_protocol.rb, line 248 248: def write_json_escape_char(ch) 249: trans.write('\\u') 250: ch_value = ch[0] 251: if (ch_value.kind_of? String) 252: ch_value = ch.bytes.first 253: end 254: trans.write(ch_value.to_s(16).rjust(4,'0')) 255: end
Convert the given integer type to a JSON number, or a string if the context requires it (eg: key in a map pair).
# File lib/thrift/protocol/json_protocol.rb, line 316 316: def write_json_integer(num) 317: @context.write(trans) 318: escapeNum = @context.escapeNum 319: if (escapeNum) 320: trans.write(@@kJSONStringDelimiter) 321: end 322: trans.write(num.to_s); 323: if (escapeNum) 324: trans.write(@@kJSONStringDelimiter) 325: end 326: end
Convert the given integer type to a JSON number, or a string if the context requires it (eg: key in a map pair).
# File lib/thrift/protocol/json_protocol.rb, line 316 316: def write_json_integer(num) 317: @context.write(trans) 318: escapeNum = @context.escapeNum 319: if (escapeNum) 320: trans.write(@@kJSONStringDelimiter) 321: end 322: trans.write(num.to_s); 323: if (escapeNum) 324: trans.write(@@kJSONStringDelimiter) 325: end 326: end
# File lib/thrift/protocol/json_protocol.rb, line 363 363: def write_json_object_end 364: pop_context 365: trans.write(@@kJSONObjectEnd) 366: end
# File lib/thrift/protocol/json_protocol.rb, line 363 363: def write_json_object_end 364: pop_context 365: trans.write(@@kJSONObjectEnd) 366: end
# File lib/thrift/protocol/json_protocol.rb, line 357 357: def write_json_object_start 358: @context.write(trans) 359: trans.write(@@kJSONObjectStart) 360: push_context(JSONPairContext.new); 361: end
# File lib/thrift/protocol/json_protocol.rb, line 357 357: def write_json_object_start 358: @context.write(trans) 359: trans.write(@@kJSONObjectStart) 360: push_context(JSONPairContext.new); 361: end
Write out the contents of the string str as a JSON string, escaping characters as appropriate.
# File lib/thrift/protocol/json_protocol.rb, line 296 296: def write_json_string(str) 297: @context.write(trans) 298: trans.write(@@kJSONStringDelimiter) 299: str.split('').each do |ch| 300: write_json_char(ch) 301: end 302: trans.write(@@kJSONStringDelimiter) 303: end
Write out the contents of the string str as a JSON string, escaping characters as appropriate.
# File lib/thrift/protocol/json_protocol.rb, line 296 296: def write_json_string(str) 297: @context.write(trans) 298: trans.write(@@kJSONStringDelimiter) 299: str.split('').each do |ch| 300: write_json_char(ch) 301: end 302: trans.write(@@kJSONStringDelimiter) 303: end
# File lib/thrift/protocol/json_protocol.rb, line 424 424: def write_list_begin(etype, size) 425: write_json_array_start 426: write_json_string(get_type_name_for_type_id(etype)) 427: write_json_integer(size) 428: end
# File lib/thrift/protocol/json_protocol.rb, line 424 424: def write_list_begin(etype, size) 425: write_json_array_start 426: write_json_string(get_type_name_for_type_id(etype)) 427: write_json_integer(size) 428: end
# File lib/thrift/protocol/json_protocol.rb, line 430 430: def write_list_end 431: write_json_array_end 432: end
# File lib/thrift/protocol/json_protocol.rb, line 430 430: def write_list_end 431: write_json_array_end 432: end
# File lib/thrift/protocol/json_protocol.rb, line 411 411: def write_map_begin(ktype, vtype, size) 412: write_json_array_start 413: write_json_string(get_type_name_for_type_id(ktype)) 414: write_json_string(get_type_name_for_type_id(vtype)) 415: write_json_integer(size) 416: write_json_object_start 417: end
# File lib/thrift/protocol/json_protocol.rb, line 411 411: def write_map_begin(ktype, vtype, size) 412: write_json_array_start 413: write_json_string(get_type_name_for_type_id(ktype)) 414: write_json_string(get_type_name_for_type_id(vtype)) 415: write_json_integer(size) 416: write_json_object_start 417: end
# File lib/thrift/protocol/json_protocol.rb, line 419 419: def write_map_end 420: write_json_object_end 421: write_json_array_end 422: end
# File lib/thrift/protocol/json_protocol.rb, line 419 419: def write_map_end 420: write_json_object_end 421: write_json_array_end 422: end
# File lib/thrift/protocol/json_protocol.rb, line 379 379: def write_message_begin(name, type, seqid) 380: write_json_array_start 381: write_json_integer(@@kThriftVersion1) 382: write_json_string(name) 383: write_json_integer(type) 384: write_json_integer(seqid) 385: end
# File lib/thrift/protocol/json_protocol.rb, line 379 379: def write_message_begin(name, type, seqid) 380: write_json_array_start 381: write_json_integer(@@kThriftVersion1) 382: write_json_string(name) 383: write_json_integer(type) 384: write_json_integer(seqid) 385: end
# File lib/thrift/protocol/json_protocol.rb, line 387 387: def write_message_end 388: write_json_array_end 389: end
# File lib/thrift/protocol/json_protocol.rb, line 387 387: def write_message_end 388: write_json_array_end 389: end
# File lib/thrift/protocol/json_protocol.rb, line 434 434: def write_set_begin(etype, size) 435: write_json_array_start 436: write_json_string(get_type_name_for_type_id(etype)) 437: write_json_integer(size) 438: end
# File lib/thrift/protocol/json_protocol.rb, line 434 434: def write_set_begin(etype, size) 435: write_json_array_start 436: write_json_string(get_type_name_for_type_id(etype)) 437: write_json_integer(size) 438: end
# File lib/thrift/protocol/json_protocol.rb, line 440 440: def write_set_end 441: write_json_array_end 442: end
# File lib/thrift/protocol/json_protocol.rb, line 440 440: def write_set_end 441: write_json_array_end 442: end
# File lib/thrift/protocol/json_protocol.rb, line 468 468: def write_string(str) 469: write_json_string(str) 470: end
# File lib/thrift/protocol/json_protocol.rb, line 468 468: def write_string(str) 469: write_json_string(str) 470: end
# File lib/thrift/protocol/json_protocol.rb, line 391 391: def write_struct_begin(name) 392: write_json_object_start 393: end
# File lib/thrift/protocol/json_protocol.rb, line 391 391: def write_struct_begin(name) 392: write_json_object_start 393: end
# File lib/thrift/protocol/json_protocol.rb, line 395 395: def write_struct_end 396: write_json_object_end 397: end