如何通过erlang读取thrift json结构?

时间:2013-04-13 15:06:56

标签: json erlang thrift

我是否必须在thrift中使用客户端/服务器框架?这是我的代码。

handle(Req, State) ->
    {Method, Req2} = cowboy_req:method(Req),
    {ok, Echo, Req3} = cowboy_req:body (Req2), %% Echo is a serialized 'hello' in smallTest by json format.
    {ok, Trans} = thrift_memory_buffer:new(Echo),
    {ok, Protocol} = thrift_json_protocol:new(Trans),
    {_Protocol1, {ok, Struct}} = thrift_protocol:read(Protocol,   smallTest_types:struct_info('hello')), %% Error here
    {ok, Req4} = echo(Method, Echo, Req3),
    {ok, Req4, State}.

堆栈跟踪:

** Stacktrace: [{thrift_json_protocol,expect,
                [{json_protocol,
                     {transport,thrift_memory_buffer,
                         {memory_buffer,
                             [<<"{\"4\":{\"str\":\"Hello World!\"}}">>]}},
                     [],undefined},
                 start_object],
                [{file,
                     "c:/Dev/server/thrift/src/thrift_json_protocol.erl"},
                 {line,349}]},
            {thrift_json_protocol,expect_many_1,4,
                [{file,
                     "c:/Dev/server/thrift/src/thrift_json_protocol.erl"},
                 {line,371}]},
            {thrift_json_protocol,expect_nodata,2,
                [{file,
                     "c:/Dev/server/thrift/src/thrift_json_protocol.erl"},
                 {line,381}]},
            {thrift_protocol,read_specific,2,
                [{file,"c:/Dev/server/thrift/src/thrift_protocol.erl"},
                 {line,186}]},
            {thrift_protocol,read,3,
                [{file,"c:/Dev/server/thrift/src/thrift_protocol.erl"},
                 {line,110}]},
            {toppage_handler,handle,2,
                [{file,"c:/Dev/server/src/toppage_handler.erl"},
                 {line,19}]},
            {cowboy_handler,handler_handle,4,
                [{file,"c:/Dev/server/cowboy/src/cowboy_handler.erl"},
                 {line,119}]},
            {cowboy_protocol,execute,4,
                [{file,"c:/Dev/server/cowboy/src/cowboy_protocol.erl"},
                 {line,514}]}]

我读了thrift_json_protocol.erl,发现thrift_protocol:read通过thrift_json_protocol:expect_nodata调用,并传递了未定义的标签,应该是jsx = {event,...}。似乎只有thrift_json_protocol:read(This0,message_begin)真的读取数据。

0 个答案:

没有答案