Skip to content

Commit fa0fb8b

Browse files
lib/rb: Add SimpleJSONProtocol
1 parent 5d267c1 commit fa0fb8b

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

lib/rb/lib/thrift/protocol/json_protocol.rb

+65-1
Original file line numberDiff line numberDiff line change
@@ -761,9 +761,73 @@ def read_binary
761761
end
762762
end
763763

764+
class SimpleJsonProtocol < JsonProtocol
765+
READ_EXCEPTION = ProtocolException.new(
766+
ProtocolException::NOT_IMPLEMENTED,
767+
'op not implemented'
768+
)
769+
770+
def write_message_begin(name, _type, _seqid)
771+
write_json_object_start
772+
write_json_string(name)
773+
end
774+
775+
def read_message_begin
776+
raise READ_EXCEPTION
777+
end
778+
779+
def write_message_end
780+
write_json_object_end
781+
end
782+
783+
def write_field_begin(name, _type, _id)
784+
write_json_string(name)
785+
end
786+
787+
def read_field_begin
788+
raise READ_EXCEPTION
789+
end
790+
791+
def write_field_end; end
792+
793+
def write_map_begin(_ktype, _vtype, _size)
794+
write_json_object_start
795+
end
796+
797+
def read_map_begin
798+
raise READ_EXCEPTION
799+
end
800+
801+
def write_map_end
802+
write_json_object_end
803+
end
804+
805+
def write_list_begin(_etype, _size)
806+
write_json_array_start
807+
end
808+
809+
def read_list_begin
810+
raise READ_EXCEPTION
811+
end
812+
813+
def write_set_begin(_etype, _size)
814+
write_json_array_start
815+
end
816+
817+
def read_set_begin
818+
raise READ_EXCEPTION
819+
end
820+
end
821+
822+
class SimpleJsonProtocolFactory < BaseProtocolFactory
823+
def get_protocol(trans)
824+
Thrift::SimpleJsonProtocol.new(trans)
825+
end
826+
end
827+
764828
class JsonProtocolFactory < BaseProtocolFactory
765829
def get_protocol(trans)
766-
return Thrift::JsonProtocol.new(trans)
830+
Thrift::JsonProtocol.new(trans)
767831
end
768832
end
769833
end

lib/rb/spec/json_protocol_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@
2121
require 'spec_helper'
2222

2323
describe 'JsonProtocol' do
24+
describe Thrift::SimpleJsonProtocol do
25+
before(:each) do
26+
@trans = Thrift::MemoryBufferTransport.new
27+
@prot = Thrift::SimpleJsonProtocol.new(@trans)
28+
end
29+
30+
it 'should pretty print object' do
31+
SpecNamespace::Hello.new.write(@prot)
32+
33+
@trans.read(@trans.available).should == '{"greeting":"hello world"}'
34+
end
35+
36+
it 'shound not be able to read message' do
37+
@trans.write('{"greeting":"hello world"}')
38+
expect {@prot.read_message_begin}.to raise_error(Thrift::ProtocolException)
39+
end
40+
end
2441

2542
describe Thrift::JsonProtocol do
2643
before(:each) do

0 commit comments

Comments
 (0)