From: Jake Farrell Date: Tue, 31 Jan 2012 03:39:30 +0000 (+0000) Subject: THRIFT-1510:There should be an implementation of the JsonProtocol for ruby X-Git-Tag: 0.9.1~451 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=6f0f52758381643ac80172d2c53c00e00c898a8e;p=common%2Fthrift.git THRIFT-1510:There should be an implementation of the JsonProtocol for ruby Client: ruby Patch: Wim Crols JSON impl for ruby git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1238144 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/rb/lib/thrift.rb b/lib/rb/lib/thrift.rb index 02d67b8b..72050b1d 100644 --- a/lib/rb/lib/thrift.rb +++ b/lib/rb/lib/thrift.rb @@ -40,6 +40,7 @@ require 'thrift/protocol/base_protocol' require 'thrift/protocol/binary_protocol' require 'thrift/protocol/binary_protocol_accelerated' require 'thrift/protocol/compact_protocol' +require 'thrift/protocol/json_protocol' # transport require 'thrift/transport/base_transport' diff --git a/lib/rb/lib/thrift/protocol/json_protocol.rb b/lib/rb/lib/thrift/protocol/json_protocol.rb new file mode 100644 index 00000000..ddbf193f --- /dev/null +++ b/lib/rb/lib/thrift/protocol/json_protocol.rb @@ -0,0 +1,756 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +@@kJSONObjectStart = '{' +@@kJSONObjectEnd = '}' +@@kJSONArrayStart = '[' +@@kJSONArrayEnd = ']' +@@kJSONNewline = '\n' +@@kJSONElemSeparator = ',' +@@kJSONPairSeparator = ':' +@@kJSONBackslash = '\\' +@@kJSONStringDelimiter = '"' + +@@kThriftVersion1 = 1 + +@@kThriftNan = "NaN" +@@kThriftInfinity = "Infinity" +@@kThriftNegativeInfinity = "-Infinity" + +module Thrift + class LookaheadReader + def initialize(trans) + @trans = trans + @hasData = false + @data = nil + end + + def read + if @hasData + @hasData = false + else + @data = @trans.read(1) + end + + return @data + end + + def peek + if !@hasData + @data = @trans.read(1) + end + @hasData = true + return @data + end + end + + # + # Class to serve as base JSON context and as base class for other context + # implementations + # + class JSONContext + # + # Write context data to the trans. Default is to do nothing. + # + def write(trans) + end + + # + # Read context data from the trans. Default is to do nothing. + # + def read(reader) + end + + # + # Return true if numbers need to be escaped as strings in this context. + # Default behavior is to return false. + # + def escapeNum + return false + end + end + + # Context class for object member key-value pairs + class JSONPairContext < JSONContext + def initialize + @first = true + @colon = true + end + + def write(trans) + if (@first) + @first = false + @colon = true + else + trans.write(@colon ? @@kJSONPairSeparator : @@kJSONElemSeparator) + @colon = !@colon + end + end + + def read(reader) + if (@first) + @first = false + @colon = true + else + ch = (@colon ? @@kJSONPairSeparator : @@kJSONElemSeparator) + @colon = !@colon + JsonProtocol::read_syntax_char(reader, ch) + end + end + + # Numbers must be turned into strings if they are the key part of a pair + def escapeNum + return @colon + end + end + + # Context class for lists + class JSONListContext < JSONContext + + def initialize + @first = true + end + + def write(trans) + if (@first) + @first = false + else + trans.write(@@kJSONElemSeparator) + end + end + + def read(reader) + if (@first) + @first = false + else + JsonProtocol::read_syntax_char(reader, @@kJSONElemSeparator) + end + end + end + + class JsonProtocol < BaseProtocol + def initialize(trans) + super(trans) + @context = JSONContext.new + @contexts = Array.new + @reader = LookaheadReader.new(trans) + end + + 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 + + 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 + + # Static helper functions + + # Read 1 character from the trans and verify that it is the expected character ch. + # Throw a protocol exception if it is not. + 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 + + # Return true if the character ch is in [-+0-9.Ee]; false otherwise + def is_json_numeric(ch) + case ch + when '+', '-', '.', '0' .. '9', 'E', "e" + return true + else + return false + end + end + + def push_context(context) + @contexts.push(@context) + @context = context + end + + def pop_context + @context = @contexts.pop + end + + # Write the character ch as a JSON escape sequence ("\u00xx") + 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 the character ch as part of a JSON string, escaping as appropriate. + def write_json_char(ch) + # This table describes the handling for the first 0x30 characters + # 0 : escape using "\u00xx" notation + # 1 : just output index + # : escape using "\" 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 out the contents of the string str as a JSON string, escaping characters as appropriate. + 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 out the contents of the string as JSON string, base64-encoding + # the string's contents, and escaping as appropriate + def write_json_base64(str) + @context.write(trans) + trans.write(@@kJSONStringDelimiter) + write_json_string([str].pack("m")) + trans.write(@@kJSONStringDelimiter) + end + + # Convert the given integer type to a JSON number, or a string + # if the context requires it (eg: key in a map pair). + 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 + + # Convert the given double to a JSON string, which is either the number, + # "NaN" or "Infinity" or "-Infinity". + 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 + + def write_json_object_start + @context.write(trans) + trans.write(@@kJSONObjectStart) + push_context(JSONPairContext.new); + end + + def write_json_object_end + pop_context + trans.write(@@kJSONObjectEnd) + end + + def write_json_array_start + @context.write(trans) + trans.write(@@kJSONArrayStart) + push_context(JSONListContext.new); + end + + def write_json_array_end + pop_context + trans.write(@@kJSONArrayEnd) + end + + 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 + + def write_message_end + write_json_array_end + end + + def write_struct_begin(name) + write_json_object_start + end + + def write_struct_end + write_json_object_end + end + + 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 + + def write_field_end + write_json_object_end + end + + def write_field_stop; nil; end + + 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 + + def write_map_end + write_json_object_end + write_json_array_end + end + + 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 + + def write_list_end + write_json_array_end + end + + 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 + + def write_set_end + write_json_array_end + end + + def write_bool(bool) + write_json_integer(bool ? 1 : 0) + end + + def write_byte(byte) + write_json_integer(byte) + end + + def write_i16(i16) + write_json_integer(i16) + end + + def write_i32(i32) + write_json_integer(i32) + end + + def write_i64(i64) + write_json_integer(i64) + end + + def write_double(dub) + write_json_double(dub) + end + + def write_string(str) + write_json_string(str) + end + + def write_binary(str) + write_json_base64(str) + end + + ## + # Reading functions + ## + + # Reads 1 byte and verifies that it matches ch. + def read_json_syntax_char(ch) + JsonProtocol::read_syntax_char(@reader, ch) + end + + # Decodes the four hex parts of a JSON escaped string character and returns + # the character via out. The first two characters must be "00". + def read_json_escape_char + read_json_syntax_char('0') + read_json_syntax_char('0') + str = @reader.read + str += @reader.read + str.hex.chr + end + + # Decodes a JSON string, including unescaping, and returns the string via str + 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 + + # Reads a block of base64 characters, decoding it, and returns via str + def read_json_base64 + read_json_string.unpack("m")[0] + end + + # Reads a sequence of characters, stopping at the first one that is not + # a valid JSON numeric character. + 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 + + # Reads a sequence of characters and assembles them into a number, + # returning them via num + 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 + + # Reads a JSON number or string and interprets it as a double. + 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 + + def read_json_object_start + @context.read(@reader) + read_json_syntax_char(@@kJSONObjectStart) + push_context(JSONPairContext.new) + nil + end + + def read_json_object_end + read_json_syntax_char(@@kJSONObjectEnd) + pop_context + nil + end + + def read_json_array_start + @context.read(@reader) + read_json_syntax_char(@@kJSONArrayStart) + push_context(JSONListContext.new) + nil + end + + def read_json_array_end + read_json_syntax_char(@@kJSONArrayEnd) + pop_context + nil + end + + 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 + + def read_message_end + read_json_array_end + nil + end + + def read_struct_begin + read_json_object_start + nil + end + + def read_struct_end + read_json_object_end + nil + end + + 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 + + def read_field_end + read_json_object_end + end + + 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 + + def read_map_end + read_json_object_end + read_json_array_end + end + + def read_list_begin + read_json_array_start + [get_type_id_for_type_name(read_json_string), read_json_integer] + end + + def read_list_end + read_json_array_end + end + + def read_set_begin + read_json_array_start + end + + def read_set_end + read_json_array_end + end + + def read_bool + byte = read_byte + byte != 0 + end + + def read_byte + read_json_integer + end + + def read_i16 + read_json_integer + end + + def read_i32 + read_json_integer + end + + def read_i64 + read_json_integer + end + + def read_double + read_json_double + end + + def read_string + read_json_string + end + + def read_binary + read_json_base64 + end + end + + class JsonProtocolFactory < BaseProtocolFactory + def get_protocol(trans) + return Thrift::JsonProtocol.new(trans) + end + end +end diff --git a/lib/rb/spec/json_protocol_spec.rb b/lib/rb/spec/json_protocol_spec.rb new file mode 100644 index 00000000..ce64aa8a --- /dev/null +++ b/lib/rb/spec/json_protocol_spec.rb @@ -0,0 +1,479 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require File.expand_path("#{File.dirname(__FILE__)}/spec_helper") + +class ThriftJsonProtocolSpec < Spec::ExampleGroup + include Thrift + + before(:each) do + @trans = Thrift::MemoryBufferTransport.new + @prot = JsonProtocol.new(@trans) + end + + it "should write json escaped char" do + @prot.write_json_escape_char("\n") + @trans.read(@trans.available).should == '\u000a' + + @prot.write_json_escape_char(" ") + @trans.read(@trans.available).should == '\u0020' + end + + it "should write json char" do + @prot.write_json_char("\n") + @trans.read(@trans.available).should == '\\n' + + @prot.write_json_char(" ") + @trans.read(@trans.available).should == ' ' + + @prot.write_json_char("\\") + @trans.read(@trans.available).should == "\\\\" + + @prot.write_json_char("@") + @trans.read(@trans.available).should == '@' + end + + it "should write json string" do + @prot.write_json_string("this is a \\ json\nstring") + @trans.read(@trans.available).should == "\"this is a \\\\ json\\nstring\"" + end + + it "should write json base64" do + @prot.write_json_base64("this is a base64 string") + @trans.read(@trans.available).should == "\"\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\\n\"\"" + end + + it "should write json integer" do + @prot.write_json_integer(45) + @trans.read(@trans.available).should == "45" + + @prot.write_json_integer(33000) + @trans.read(@trans.available).should == "33000" + + @prot.write_json_integer(3000000000) + @trans.read(@trans.available).should == "3000000000" + + @prot.write_json_integer(6000000000) + @trans.read(@trans.available).should == "6000000000" + end + + it "should write json double" do + @prot.write_json_double(12.3) + @trans.read(@trans.available).should == "12.3" + + @prot.write_json_double(-3.21) + @trans.read(@trans.available).should == "-3.21" + + @prot.write_json_double(((+1.0/0.0)/(+1.0/0.0))) + @trans.read(@trans.available).should == "\"NaN\"" + + @prot.write_json_double((+1.0/0.0)) + @trans.read(@trans.available).should == "\"Infinity\"" + + @prot.write_json_double((-1.0/0.0)) + @trans.read(@trans.available).should == "\"-Infinity\"" + end + + it "should write json object start" do + @prot.write_json_object_start + @trans.read(@trans.available).should == "{" + end + + it "should write json object end" do + @prot.write_json_object_end + @trans.read(@trans.available).should == "}" + end + + it "should write json array start" do + @prot.write_json_array_start + @trans.read(@trans.available).should == "[" + end + + it "should write json array end" do + @prot.write_json_array_end + @trans.read(@trans.available).should == "]" + end + + it "should write message begin" do + @prot.write_message_begin("name", 12, 32) + @trans.read(@trans.available).should == "[1,\"name\",12,32" + end + + it "should write message end" do + @prot.write_message_end + @trans.read(@trans.available).should == "]" + end + + it "should write struct begin" do + @prot.write_struct_begin("name") + @trans.read(@trans.available).should == "{" + end + + it "should write struct end" do + @prot.write_struct_end + @trans.read(@trans.available).should == "}" + end + + it "should write field begin" do + @prot.write_field_begin("name", Types::STRUCT, 32) + @trans.read(@trans.available).should == "32{\"rec\"" + end + + it "should write field end" do + @prot.write_field_end + @trans.read(@trans.available).should == "}" + end + + it "should write field stop" do + @prot.write_field_stop + @trans.read(@trans.available).should == "" + end + + it "should write map begin" do + @prot.write_map_begin(Types::STRUCT, Types::LIST, 32) + @trans.read(@trans.available).should == "[\"rec\",\"lst\",32,{" + end + + it "should write map end" do + @prot.write_map_end + @trans.read(@trans.available).should == "}]" + end + + it "should write list begin" do + @prot.write_list_begin(Types::STRUCT, 32) + @trans.read(@trans.available).should == "[\"rec\",32" + end + + it "should write list end" do + @prot.write_list_end + @trans.read(@trans.available).should == "]" + end + + it "should write set begin" do + @prot.write_set_begin(Types::STRUCT, 32) + @trans.read(@trans.available).should == "[\"rec\",32" + end + + it "should write set end" do + @prot.write_set_end + @trans.read(@trans.available).should == "]" + end + + it "should write bool" do + @prot.write_bool(true) + @trans.read(@trans.available).should == "1" + + @prot.write_bool(false) + @trans.read(@trans.available).should == "0" + end + + it "should write byte" do + @prot.write_byte(100) + @trans.read(@trans.available).should == "100" + end + + it "should write i16" do + @prot.write_i16(1000) + @trans.read(@trans.available).should == "1000" + end + + it "should write i32" do + @prot.write_i32(3000000000) + @trans.read(@trans.available).should == "3000000000" + end + + it "should write i64" do + @prot.write_i64(6000000000) + @trans.read(@trans.available).should == "6000000000" + end + + it "should write double" do + @prot.write_double(1.23) + @trans.read(@trans.available).should == "1.23" + + @prot.write_double(-32.1) + @trans.read(@trans.available).should == "-32.1" + + @prot.write_double(((+1.0/0.0)/(+1.0/0.0))) + @trans.read(@trans.available).should == "\"NaN\"" + + @prot.write_double((+1.0/0.0)) + @trans.read(@trans.available).should == "\"Infinity\"" + + @prot.write_double((-1.0/0.0)) + @trans.read(@trans.available).should == "\"-Infinity\"" + end + + it "should write string" do + @prot.write_string("this is a test string") + @trans.read(@trans.available).should == "\"this is a test string\"" + end + + it "should write binary" do + @prot.write_binary("this is a base64 string") + @trans.read(@trans.available).should == "\"\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\\n\"\"" + end + + it "should get type name for type id" do + expect {@prot.get_type_name_for_type_id(Types::STOP)}.to raise_error(NotImplementedError) + expect {@prot.get_type_name_for_type_id(Types::VOID)}.to raise_error(NotImplementedError) + @prot.get_type_name_for_type_id(Types::BOOL).should == "tf" + @prot.get_type_name_for_type_id(Types::BYTE).should == "i8" + @prot.get_type_name_for_type_id(Types::DOUBLE).should == "dbl" + @prot.get_type_name_for_type_id(Types::I16).should == "i16" + @prot.get_type_name_for_type_id(Types::I32).should == "i32" + @prot.get_type_name_for_type_id(Types::I64).should == "i64" + @prot.get_type_name_for_type_id(Types::STRING).should == "str" + @prot.get_type_name_for_type_id(Types::STRUCT).should == "rec" + @prot.get_type_name_for_type_id(Types::MAP).should == "map" + @prot.get_type_name_for_type_id(Types::SET).should == "set" + @prot.get_type_name_for_type_id(Types::LIST).should == "lst" + end + + it "should get type id for type name" do + expect {@prot.get_type_id_for_type_name("pp")}.to raise_error(NotImplementedError) + @prot.get_type_id_for_type_name("tf").should == Types::BOOL + @prot.get_type_id_for_type_name("i8").should == Types::BYTE + @prot.get_type_id_for_type_name("dbl").should == Types::DOUBLE + @prot.get_type_id_for_type_name("i16").should == Types::I16 + @prot.get_type_id_for_type_name("i32").should == Types::I32 + @prot.get_type_id_for_type_name("i64").should == Types::I64 + @prot.get_type_id_for_type_name("str").should == Types::STRING + @prot.get_type_id_for_type_name("rec").should == Types::STRUCT + @prot.get_type_id_for_type_name("map").should == Types::MAP + @prot.get_type_id_for_type_name("set").should == Types::SET + @prot.get_type_id_for_type_name("lst").should == Types::LIST + end + + it "should read json syntax char" do + @trans.write('F') + expect {@prot.read_json_syntax_char('G')}.to raise_error(ProtocolException) + @trans.write('H') + @prot.read_json_syntax_char('H') + end + + it "should read json escape char" do + @trans.write('0054') + @prot.read_json_escape_char.should == 'T' + end + + it "should read json string" do + @trans.write("\"\\P") + expect {@prot.read_json_string(false)}.to raise_error(ProtocolException) + + @trans.write("\"\\n\"") + @prot.read_json_string(false).should == "\\n" + + @trans.write("\"this is a test string\"") + @prot.read_json_string.should == "this is a test string" + end + + it "should read json base64" do + @trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"") + @prot.read_json_base64.should == "this is a test string" + end + + it "should is json numeric" do + @prot.is_json_numeric("A").should == false + @prot.is_json_numeric("+").should == true + @prot.is_json_numeric("-").should == true + @prot.is_json_numeric(".").should == true + @prot.is_json_numeric("0").should == true + @prot.is_json_numeric("1").should == true + @prot.is_json_numeric("2").should == true + @prot.is_json_numeric("3").should == true + @prot.is_json_numeric("4").should == true + @prot.is_json_numeric("5").should == true + @prot.is_json_numeric("6").should == true + @prot.is_json_numeric("7").should == true + @prot.is_json_numeric("8").should == true + @prot.is_json_numeric("9").should == true + @prot.is_json_numeric("E").should == true + @prot.is_json_numeric("e").should == true + end + + it "should read json numeric chars" do + @trans.write("1.453E45T") + @prot.read_json_numeric_chars.should == "1.453E45" + end + + it "should read json integer" do + @trans.write("1.45\"\"") + expect {@prot.read_json_integer}.to raise_error(ProtocolException) + @prot.read_string + + @trans.write("1453T") + @prot.read_json_integer.should == 1453 + end + + it "should read json double" do + @trans.write("1.45e3e01\"\"") + expect {@prot.read_json_double}.to raise_error(ProtocolException) + @prot.read_string + + @trans.write("\"1.453e01\"") + expect {@prot.read_json_double}.to raise_error(ProtocolException) + + @trans.write("1.453e01\"\"") + @prot.read_json_double.should == 14.53 + @prot.read_string + + @trans.write("\"NaN\"") + @prot.read_json_double.nan?.should == true + + @trans.write("\"Infinity\"") + @prot.read_json_double.should == +1.0/0.0 + + @trans.write("\"-Infinity\"") + @prot.read_json_double.should == -1.0/0.0 + end + + it "should read json object start" do + @trans.write("{") + @prot.read_json_object_start.should == nil + end + + it "should read json object end" do + @trans.write("}") + @prot.read_json_object_end.should == nil + end + + it "should read json array start" do + @trans.write("[") + @prot.read_json_array_start.should == nil + end + + it "should read json array end" do + @trans.write("]") + @prot.read_json_array_end.should == nil + end + + it "should read_message_begin" do + @trans.write("[2,") + expect {@prot.read_message_begin}.to raise_error(ProtocolException) + + @trans.write("[1,\"name\",12,32\"\"") + @prot.read_message_begin.should == ["name", 12, 32] + end + + it "should read message end" do + @trans.write("]") + @prot.read_message_end.should == nil + end + + it "should read struct begin" do + @trans.write("{") + @prot.read_struct_begin.should == nil + end + + it "should read struct end" do + @trans.write("}") + @prot.read_struct_end.should == nil + end + + it "should read field begin" do + @trans.write("1{\"rec\"") + @prot.read_field_begin.should == [nil, 12, 1] + end + + it "should read field end" do + @trans.write("}") + @prot.read_field_end.should == nil + end + + it "should read map begin" do + @trans.write("[\"rec\",\"lst\",2,{") + @prot.read_map_begin.should == [12, 15, 2] + end + + it "should read map end" do + @trans.write("}]") + @prot.read_map_end.should == nil + end + + it "should read list begin" do + @trans.write("[\"rec\",2\"\"") + @prot.read_list_begin.should == [12, 2] + end + + it "should read list end" do + @trans.write("]") + @prot.read_list_end.should == nil + end + + it "should read set begin" do + @trans.write("[") + @prot.read_set_begin.should == nil + end + + it "should read set end" do + @trans.write("]") + @prot.read_set_end.should == nil + end + + it "should read bool" do + @trans.write("0\"\"") + @prot.read_bool.should == false + @prot.read_string + + @trans.write("1\"\"") + @prot.read_bool.should == true + end + + it "should read byte" do + @trans.write("60\"\"") + @prot.read_byte.should == 60 + end + + it "should read i16" do + @trans.write("1000\"\"") + @prot.read_i16.should == 1000 + end + + it "should read i32" do + @trans.write("3000000000\"\"") + @prot.read_i32.should == 3000000000 + end + + it "should read i64" do + @trans.write("6000000000\"\"") + @prot.read_i64.should == 6000000000 + end + + it "should read double" do + @trans.write("12.23\"\"") + @prot.read_double.should == 12.23 + end + + it "should read string" do + @trans.write("\"this is a test string\"") + @prot.read_string.should == "this is a test string" + end + + it "should read binary" do + @trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"") + @prot.read_binary.should == "this is a test string" + end + + describe JsonProtocolFactory do + it "should create a JsonProtocol" do + JsonProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(JsonProtocol) + end + end +end