Thrift::JsonProtocol

Public Class Methods

new(trans) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 148
def initialize(trans)
  super(trans)
  @context = JSONContext.new
  @contexts = Array.new
  @reader = LookaheadReader.new(trans)
end
read_syntax_char(reader, ch) click to toggle source

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 220
def self.read_syntax_char(reader, ch)
  ch2 = reader.read
  if (ch2 != ch)
    raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected \'#{ch}\' got \'#{ch2}\'.")
  end
end

Public Instance Methods

get_type_id_for_type_name(name) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 184
def get_type_id_for_type_name(name)
  if (name == "tf")
    result = Types::BOOL
  elsif (name == "i8")
    result = Types::BYTE
  elsif (name == "i16")
    result = Types::I16
  elsif (name == "i32")
    result = Types::I32
  elsif (name == "i64")
    result = Types::I64
  elsif (name == "dbl")
    result = Types::DOUBLE
  elsif (name == "str")
    result = Types::STRING
  elsif (name == "rec")
    result = Types::STRUCT
  elsif (name == "map")
    result = Types::MAP
  elsif (name == "set")
    result = Types::SET
  elsif (name == "lst")
    result = Types::LIST
  else
    result = Types::STOP
  end
  if (result == Types::STOP)
    raise NotImplementedError
  end
  return result
end
get_type_name_for_type_id(id) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 155
def get_type_name_for_type_id(id)
  case id
  when Types::BOOL
    "tf"
  when Types::BYTE
    "i8"
  when Types::I16
    "i16"
  when Types::I32
    "i32"
  when Types::I64
    "i64"
  when Types::DOUBLE
    "dbl"
  when Types::STRING
    "str"
  when Types::STRUCT
    "rec"
  when Types::MAP
    "map"
  when Types::SET
    "set"
  when Types::LIST
    "lst"
  else
    raise NotImplementedError
  end
end
is_json_numeric(ch) click to toggle source

Return true if the character ch is in [-+0-9.Ee]; false otherwise

# File lib/thrift/protocol/json_protocol.rb, line 228
def is_json_numeric(ch)
  case ch
  when '+', '-', '.', '0' .. '9', 'E', "e"
    return true
  else
    return false
  end
end
pop_context() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 242
def pop_context
  @context = @contexts.pop
end
push_context(context) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 237
def push_context(context)
  @contexts.push(@context)
  @context = context
end
read_binary() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 754
def read_binary
  read_json_base64
end
read_bool() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 725
def read_bool
  byte = read_byte
  byte != 0
end
read_byte() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 730
def read_byte
  read_json_integer
end
read_double() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 746
def read_double
  read_json_double
end
read_field_begin() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 677
def read_field_begin
  # Check if we hit the end of the list
  ch = @reader.peek
  if (ch == @@kJSONObjectEnd)
    field_type = Types::STOP
  else
    field_id = read_json_integer
    read_json_object_start
    field_type = get_type_id_for_type_name(read_json_string)
  end
  [nil, field_type, field_id]
end
read_field_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 690
def read_field_end
  read_json_object_end
end
read_i16() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 734
def read_i16
  read_json_integer
end
read_i32() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 738
def read_i32
  read_json_integer
end
read_i64() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 742
def read_i64
  read_json_integer
end
read_json_array_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 644
def read_json_array_end
  read_json_syntax_char(@@kJSONArrayEnd)
  pop_context
  nil
end
read_json_array_start() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 637
def read_json_array_start
  @context.read(@reader)
  read_json_syntax_char(@@kJSONArrayStart)
  push_context(JSONListContext.new)
  nil
end
read_json_base64() click to toggle source

Reads a block of base64 characters, decoding it, and returns via str

# File lib/thrift/protocol/json_protocol.rb, line 544
def read_json_base64
  read_json_string.unpack("m")[0]
end
read_json_double() click to toggle source

Reads a JSON number or string and interprets it as a double.

# File lib/thrift/protocol/json_protocol.rb, line 586
def read_json_double
  @context.read(@reader)
  num = 0
  if (@reader.peek == @@kJSONStringDelimiter)
    str = read_json_string(true)
    # Check for NaN, Infinity and -Infinity
    if (str == @@kThriftNan)
      num = (+1.0/0.0)/(+1.0/0.0)
    elsif (str == @@kThriftInfinity)
      num = +1.0/0.0
    elsif (str == @@kThriftNegativeInfinity)
      num = -1.0/0.0
    else
      if (!@context.escapeNum)
        # Raise exception -- we should not be in a string in this case
        raise ProtocolException.new(ProtocolException::INVALID_DATA, "Numeric data unexpectedly quoted")
      end
      begin
        num = Float(str)
      rescue
        raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected numeric value; got \"#{str}\"")
      end
    end
  else
    if (@context.escapeNum)
      # This will throw - we should have had a quote if escapeNum == true
      read_json_syntax_char(@@kJSONStringDelimiter)
    end
    str = read_json_numeric_chars
    begin
      num = Float(str)
    rescue
      raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected numeric value; got \"#{str}\"")
    end
  end
  return num
end
read_json_escape_char() click to toggle source

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 490
def read_json_escape_char
  str = @reader.read
  str += @reader.read
  str += @reader.read
  str += @reader.read
  if RUBY_VERSION >= '1.9'
    str.hex.chr(Encoding::UTF_8)
  else
    str.hex.chr
  end
end
read_json_integer() click to toggle source

Reads a sequence of characters and assembles them into a number, returning them via num

# File lib/thrift/protocol/json_protocol.rb, line 565
def read_json_integer
  @context.read(@reader)
  if (@context.escapeNum)
    read_json_syntax_char(@@kJSONStringDelimiter)
  end
  str = read_json_numeric_chars

  begin
    num = Integer(str);
  rescue
    raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected numeric value; got \"#{str}\"")
  end

  if (@context.escapeNum)
    read_json_syntax_char(@@kJSONStringDelimiter)
  end

  return num
end
read_json_numeric_chars() click to toggle source

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 550
def read_json_numeric_chars
  str = ""
  while (true)
    ch = @reader.peek
    if (!is_json_numeric(ch))
      break;
    end
    ch = @reader.read
    str += ch
  end
  return str
end
read_json_object_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 631
def read_json_object_end
  read_json_syntax_char(@@kJSONObjectEnd)
  pop_context
  nil
end
read_json_object_start() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 624
def read_json_object_start
  @context.read(@reader)
  read_json_syntax_char(@@kJSONObjectStart)
  push_context(JSONPairContext.new)
  nil
end
read_json_string(skipContext = false) click to toggle source

Decodes a JSON string, including unescaping, and returns the string via str

# File lib/thrift/protocol/json_protocol.rb, line 503
def read_json_string(skipContext = false)
  # This string's characters must match up with the elements in escape_char_vals.
  # I don't have '/' on this list even though it appears on www.json.org --
  # it is not in the RFC
  escape_chars = "\"\\bfnrt"

  # The elements of this array must match up with the sequence of characters in
  # escape_chars
  escape_char_vals = [
    '"', '\', '\b', '\f', '\n', '\r', '\t',
  ]

  if !skipContext
    @context.read(@reader)
  end
  read_json_syntax_char(@@kJSONStringDelimiter)
  ch = ""
  str = ""
  while (true)
    ch = @reader.read
    if (ch == @@kJSONStringDelimiter)
      break
    end
    if (ch == @@kJSONBackslash)
      ch = @reader.read
      if (ch == 'u')
        ch = read_json_escape_char
      else
        pos = escape_chars.index(ch);
        if (pos.nil?) # not found
          raise ProtocolException.new(ProtocolException::INVALID_DATA, "Expected control char, got \'#{ch}\'.")
        end
        ch = escape_char_vals[pos]
      end
    end
    str += ch
  end
  return str
end
read_json_syntax_char(ch) click to toggle source

Reads 1 byte and verifies that it matches ch.

# File lib/thrift/protocol/json_protocol.rb, line 480
def read_json_syntax_char(ch)
  JsonProtocol::read_syntax_char(@reader, ch)
end
read_list_begin() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 708
def read_list_begin
  read_json_array_start
  [get_type_id_for_type_name(read_json_string), read_json_integer]
end
read_list_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 713
def read_list_end
  read_json_array_end
end
read_map_begin() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 694
def read_map_begin
  read_json_array_start
  key_type = get_type_id_for_type_name(read_json_string)
  val_type = get_type_id_for_type_name(read_json_string)
  size = read_json_integer
  read_json_object_start
  [key_type, val_type, size]
end
read_map_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 703
def read_map_end
  read_json_object_end
  read_json_array_end
end
read_message_begin() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 650
def read_message_begin
  read_json_array_start
  version = read_json_integer
  if (version != @@kThriftVersion1)
    raise ProtocolException.new(ProtocolException::BAD_VERSION, 'Message contained bad version.')
  end
  name = read_json_string
  message_type = read_json_integer
  seqid = read_json_integer
  [name, message_type, seqid]
end
read_message_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 662
def read_message_end
  read_json_array_end
  nil
end
read_set_begin() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 717
def read_set_begin
  read_json_array_start
end
read_set_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 721
def read_set_end
  read_json_array_end
end
read_string() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 750
def read_string
  read_json_string
end
read_struct_begin() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 667
def read_struct_begin
  read_json_object_start
  nil
end
read_struct_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 672
def read_struct_end
  read_json_object_end
  nil
end
write_binary(str) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 471
def write_binary(str)
  write_json_base64(str)
end
write_bool(bool) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 443
def write_bool(bool)
  write_json_integer(bool ? 1 : 0)
end
write_byte(byte) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 447
def write_byte(byte)
  write_json_integer(byte)
end
write_double(dub) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 463
def write_double(dub)
  write_json_double(dub)
end
write_field_begin(name, type, id) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 398
def write_field_begin(name, type, id)
  write_json_integer(id)
  write_json_object_start
  write_json_string(get_type_name_for_type_id(type))
end
write_field_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 404
def write_field_end
  write_json_object_end
end
write_field_stop() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 408
def write_field_stop; nil; end
write_i16(i16) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 451
def write_i16(i16)
  write_json_integer(i16)
end
write_i32(i32) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 455
def write_i32(i32)
  write_json_integer(i32)
end
write_i64(i64) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 459
def write_i64(i64)
  write_json_integer(i64)
end
write_json_array_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 373
def write_json_array_end
  pop_context
  trans.write(@@kJSONArrayEnd)
end
write_json_array_start() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 367
def write_json_array_start
  @context.write(trans)
  trans.write(@@kJSONArrayStart)
  push_context(JSONListContext.new);
end
write_json_base64(str) click to toggle source

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 306
def write_json_base64(str)
  @context.write(trans)
  trans.write(@@kJSONStringDelimiter)
  write_json_string([str].pack("m"))
  trans.write(@@kJSONStringDelimiter)
end
write_json_char(ch) click to toggle source

Write the character ch as part of a JSON string, escaping as appropriate.

# File lib/thrift/protocol/json_protocol.rb, line 257
def write_json_char(ch)
  # This table describes the handling for the first 0x30 characters
  # 0 : escape using "\u00xx" notation
  # 1 : just output index
  # <other> : escape using "\<other>" notation
  kJSONCharTable = [
      # 0 1 2 3 4 5 6 7 8 9 A B C D E F
      0, 0, 0, 0, 0, 0, 0, 0,'b','t','n', 0,'f','r', 0, 0, # 0
      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # 1
      1, 1,'"', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, # 2
  ]

  ch_value = ch[0]
  if (ch_value.kind_of? String)
    ch_value = ch.bytes.first
  end
  if (ch_value >= 0x30)
    if (ch == @@kJSONBackslash) # Only special character >= 0x30 is '\'
      trans.write(@@kJSONBackslash)
      trans.write(@@kJSONBackslash)
    else
      trans.write(ch)
    end
  else
    outCh = kJSONCharTable[ch_value];
    # Check if regular character, backslash escaped, or JSON escaped
    if outCh.kind_of? String
      trans.write(@@kJSONBackslash)
      trans.write(outCh)
    elsif outCh == 1
      trans.write(ch)
    else
      write_json_escape_char(ch)
    end
  end
end
write_json_double(num) click to toggle source

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 329
def write_json_double(num)
  @context.write(trans)
  # Normalize output of boost::lexical_cast for NaNs and Infinities
  special = false;
  if (num.nan?)
    special = true;
    val = @@kThriftNan;
  elsif (num.infinite?)
    special = true;
    val = @@kThriftInfinity;
    if (num < 0.0)
      val = @@kThriftNegativeInfinity;
    end
  else
    val = num.to_s
  end

  escapeNum = special || @context.escapeNum
  if (escapeNum)
    trans.write(@@kJSONStringDelimiter)
  end
  trans.write(val)
  if (escapeNum)
    trans.write(@@kJSONStringDelimiter)
  end
end
write_json_escape_char(ch) click to toggle source

Write the character ch as a JSON escape sequence ("u00xx")

# File lib/thrift/protocol/json_protocol.rb, line 247
def write_json_escape_char(ch)
  trans.write('\u')
  ch_value = ch[0]
  if (ch_value.kind_of? String)
    ch_value = ch.bytes.first
  end
  trans.write(ch_value.to_s(16).rjust(4,'0'))
end
write_json_integer(num) click to toggle source

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 315
def write_json_integer(num)
  @context.write(trans)
  escapeNum = @context.escapeNum
  if (escapeNum)
    trans.write(@@kJSONStringDelimiter)
  end
  trans.write(num.to_s);
  if (escapeNum)
    trans.write(@@kJSONStringDelimiter)
  end
end
write_json_object_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 362
def write_json_object_end
  pop_context
  trans.write(@@kJSONObjectEnd)
end
write_json_object_start() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 356
def write_json_object_start
  @context.write(trans)
  trans.write(@@kJSONObjectStart)
  push_context(JSONPairContext.new);
end
write_json_string(str) click to toggle source

Write out the contents of the string str as a JSON string, escaping characters as appropriate.

# File lib/thrift/protocol/json_protocol.rb, line 295
def write_json_string(str)
  @context.write(trans)
  trans.write(@@kJSONStringDelimiter)
  str.split('').each do |ch|
    write_json_char(ch)
  end
  trans.write(@@kJSONStringDelimiter)
end
write_list_begin(etype, size) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 423
def write_list_begin(etype, size)
  write_json_array_start
  write_json_string(get_type_name_for_type_id(etype))
  write_json_integer(size)
end
write_list_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 429
def write_list_end
  write_json_array_end
end
write_map_begin(ktype, vtype, size) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 410
def write_map_begin(ktype, vtype, size)
  write_json_array_start
  write_json_string(get_type_name_for_type_id(ktype))
  write_json_string(get_type_name_for_type_id(vtype))
  write_json_integer(size)
  write_json_object_start
end
write_map_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 418
def write_map_end
  write_json_object_end
  write_json_array_end
end
write_message_begin(name, type, seqid) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 378
def write_message_begin(name, type, seqid)
  write_json_array_start
  write_json_integer(@@kThriftVersion1)
  write_json_string(name)
  write_json_integer(type)
  write_json_integer(seqid)
end
write_message_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 386
def write_message_end
  write_json_array_end
end
write_set_begin(etype, size) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 433
def write_set_begin(etype, size)
  write_json_array_start
  write_json_string(get_type_name_for_type_id(etype))
  write_json_integer(size)
end
write_set_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 439
def write_set_end
  write_json_array_end
end
write_string(str) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 467
def write_string(str)
  write_json_string(str)
end
write_struct_begin(name) click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 390
def write_struct_begin(name)
  write_json_object_start
end
write_struct_end() click to toggle source
# File lib/thrift/protocol/json_protocol.rb, line 394
def write_struct_end
  write_json_object_end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.