thrift序列化和反序列化

来源:互联网 发布:mac卸载java8 编辑:程序博客网 时间:2024/05/08 00:05

转自 http://blog.csdn.net/hbuxiaoshe/article/details/8517528


thrift序列化和反序列化很简单,写个例子如下:

[cpp] view plaincopy
  1. #include <transport/TSocket.h>    
  2. #include <transport/TBufferTransports.h>    
  3. #include <protocol/TBinaryProtocol.h>    
  4.   
  5. #include "gen-cpp/student_types.h"  
  6.   
  7. using namespace apache::thrift;    
  8. using namespace apache::thrift::protocol;    
  9. using namespace apache::thrift::transport;    
  10.     
  11. using boost::shared_ptr;   
  12.   
  13. int main()  
  14. {  
  15.   // student的结构来自http://blog.csdn.net/hbuxiaoshe/article/details/6558391中的thrift  
  16.   Student s1;  
  17.   s1.__set_sno(1);  
  18.   s1.__set_sname("Hello");  
  19.   s1.__set_ssex(false);  
  20.   s1.__set_sage(21);  
  21.   
  22.   // 序列化  
  23.   uint8_t* buf_ptr;  
  24.   uint32_t sz;  
  25.   shared_ptr<TMemoryBuffer> mem_buf(new TMemoryBuffer);  
  26.   shared_ptr<TBinaryProtocol> bin_proto(new TBinaryProtocol(mem_buf));  
  27.   s1.write(bin_proto.get());  
  28.   mem_buf->getBuffer(&buf_ptr, &sz);  
  29.   
  30.   // 反序列化  
  31.   Student s2;  
  32.   shared_ptr<TMemoryBuffer> membuffer(new TMemoryBuffer());  
  33.   shared_ptr<TProtocol> protocol(new TBinaryProtocol(membuffer));  
  34.   membuffer->resetBuffer(buf_ptr, sz);  
  35.   s2.read(protocol.get());  
  36.   printf("%d %s %d %d\n", s2.sno, s2.sname.c_str(), s2.ssex, s2.sage);  
  37.   
  38.   return 0;  
  39. }  
编译命令:

g++ -g -o s s.cpp gen-cpp/student_constants.cpp gen-cpp/student_types.cpp \
-Lxxx/lib -Ixxx//include/thrift -lthrift 

序列化还可以这样实现:

sz = s1.write(bin_proto.get());

string buf = mem_buf->getBufferAsString();


具体使用可查看TMemoryBuffer的接口,在文件include/thrift/transport/TBufferTransports.h中。


补充个易用模版:

[cpp] view plaincopy
  1. #ifndef SERIALIZE_H_  
  2. #define SERIALIZE_H_  
  3.   
  4. #include <config.h>  
  5. #include <protocol/TBinaryProtocol.h>  
  6. #include <transport/TTransportUtils.h>  
  7.   
  8. template<typename ThriftStruct>  
  9. std::string ThriftToString(const ThriftStruct& ts) {  
  10.   using namespace apache::thrift::transport;  // NOLINT  
  11.   using namespace apache::thrift::protocol;  // NOLINT  
  12.   TMemoryBuffer* buffer = new TMemoryBuffer;  
  13.   boost::shared_ptr<TTransport> trans(buffer);  
  14.   TBinaryProtocol protocol(trans);  
  15.   ts.write(&protocol);  
  16.   uint8_t* buf;  
  17.   uint32_t size;  
  18.   buffer->getBuffer(&buf, &size);  
  19.   return std::string((char*)buf, (unsigned int)size);  // NOLINT  
  20. }  
  21.   
  22. template<typename ThriftStruct>  
  23. bool StringToThrift(const std::string& buff,  
  24.                     ThriftStruct* ts) {  
  25.   using namespace apache::thrift::transport;  // NOLINT  
  26.   using namespace apache::thrift::protocol;  // NOLINT  
  27.   TMemoryBuffer* buffer = new TMemoryBuffer;  
  28.   buffer->write((const uint8_t*)buff.data(), buff.size());  
  29.   boost::shared_ptr<TTransport> trans(buffer);  
  30.   TBinaryProtocol protocol(trans);  
  31.   ts->read(&protocol);  
  32.   return true;  
  33. }  
  34.   
  35. #endif  // SERIALIZE_H_  
0 0
原创粉丝点击