thrift序列化和反序列化
来源:互联网 发布:mac卸载java8 编辑:程序博客网 时间:2024/05/08 00:05
转自 http://blog.csdn.net/hbuxiaoshe/article/details/8517528
thrift序列化和反序列化很简单,写个例子如下:
- #include <transport/TSocket.h>
- #include <transport/TBufferTransports.h>
- #include <protocol/TBinaryProtocol.h>
- #include "gen-cpp/student_types.h"
- using namespace apache::thrift;
- using namespace apache::thrift::protocol;
- using namespace apache::thrift::transport;
- using boost::shared_ptr;
- int main()
- {
- // student的结构来自http://blog.csdn.net/hbuxiaoshe/article/details/6558391中的thrift
- Student s1;
- s1.__set_sno(1);
- s1.__set_sname("Hello");
- s1.__set_ssex(false);
- s1.__set_sage(21);
- // 序列化
- uint8_t* buf_ptr;
- uint32_t sz;
- shared_ptr<TMemoryBuffer> mem_buf(new TMemoryBuffer);
- shared_ptr<TBinaryProtocol> bin_proto(new TBinaryProtocol(mem_buf));
- s1.write(bin_proto.get());
- mem_buf->getBuffer(&buf_ptr, &sz);
- // 反序列化
- Student s2;
- shared_ptr<TMemoryBuffer> membuffer(new TMemoryBuffer());
- shared_ptr<TProtocol> protocol(new TBinaryProtocol(membuffer));
- membuffer->resetBuffer(buf_ptr, sz);
- s2.read(protocol.get());
- printf("%d %s %d %d\n", s2.sno, s2.sname.c_str(), s2.ssex, s2.sage);
- return 0;
- }
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中。
补充个易用模版:
- #ifndef SERIALIZE_H_
- #define SERIALIZE_H_
- #include <config.h>
- #include <protocol/TBinaryProtocol.h>
- #include <transport/TTransportUtils.h>
- template<typename ThriftStruct>
- std::string ThriftToString(const ThriftStruct& ts) {
- using namespace apache::thrift::transport; // NOLINT
- using namespace apache::thrift::protocol; // NOLINT
- TMemoryBuffer* buffer = new TMemoryBuffer;
- boost::shared_ptr<TTransport> trans(buffer);
- TBinaryProtocol protocol(trans);
- ts.write(&protocol);
- uint8_t* buf;
- uint32_t size;
- buffer->getBuffer(&buf, &size);
- return std::string((char*)buf, (unsigned int)size); // NOLINT
- }
- template<typename ThriftStruct>
- bool StringToThrift(const std::string& buff,
- ThriftStruct* ts) {
- using namespace apache::thrift::transport; // NOLINT
- using namespace apache::thrift::protocol; // NOLINT
- TMemoryBuffer* buffer = new TMemoryBuffer;
- buffer->write((const uint8_t*)buff.data(), buff.size());
- boost::shared_ptr<TTransport> trans(buffer);
- TBinaryProtocol protocol(trans);
- ts->read(&protocol);
- return true;
- }
- #endif // SERIALIZE_H_
0 0
- thrift序列化和反序列化
- thrift序列化和反序列化
- Thrift序列化与反序列化
- python 使用thrift序列化与反序列化对象
- python 使用thrift序列化与反序列化对象
- thrift框架 序列化及反序列化解析
- 序列和反序列化
- 序列和反序列化
- Thrift序列化和反序列化处理代码(java版)
- 序列化和反序列化
- 序列化和反序列化
- 序列化和反序列化
- c# 序列化和反序列化
- .NET序列化和反序列化
- .net序列化和反序列化
- 序列化和反序列化
- C#序列化和反序列化
- XML序列化和反序列化
- GTD
- xcode中同一project创建多个target
- Jquery常用方法
- Lua中的元方法__newindex详解
- android 自定义组合控件
- thrift序列化和反序列化
- 小白上手 为Kindle添加图书的五种方法
- 随机打算扑克顺序
- 【工作日志】0006利用Ajax或者form表单从前台传值到后台
- Lua中__index和__newindex之间的沉默与合作
- activity切换的时候添加动画效果
- RecyclerView的notifyItemRemoved()方法删除条目问题
- IT女精英 李然
- Android 使用android-support-multidex解决Dex超出方法数的限制问题,让你的应用不再爆棚