Google ProtoBuf开发示例
来源:互联网 发布:淘宝联盟推广赚钱技巧 编辑:程序博客网 时间:2024/05/22 20:38
一、简介
ProtocolBuffer是用于结构化数据串行化的灵活、高效、自动的方法,有如XML,不过它更小、更快、也更简单。你可以定义自己的数据结构,然后使用代码生成器生成的代码来读写这个数据结构。你甚至可以在无需重新部署程序的情况下更新数据结构。
二、特点
ProtocolBuffer拥有多项比XML更高级的串行化结构数据的特性,ProtocolBuffer:
· 更简单
· 小3-10倍
· 快20-100倍
· 更少的歧义
· 可以方便的生成数据存取类
例如,让我们看看如何在XML中建模Person的name和email字段:
<person>
<name>John Doe</name>
<email>jdoe@example.com</email>
</person>
对应的ProtocolBuffer报文则如下:
#ProtocolBuffer的文本表示
#这不是正常时使用的二进制数据
person {
name: "John Doe"
email: "jdoe@example.com"
}
三、开发步骤
1、下载包( http://code.google.com/p/protobuf/downloads/),包含了Java、Python、C++的ProtocolBuffer编译器,用于生成你需要的IO类。构建和安装你的编译器,跟随README的指令就可以做到。
一旦你安装好了,就可以跟着编程指导(http://code.google.com/apis/protocolbuffers/docs/tutorials.html )来选择语言-随后就是使用ProtocolBuffer创建一个简单的应用了。
2、创建. proto文件,文件中定义你需要做串行化的数据结构信息,下面定义个Order.Proto:
packagexquant;optionjava_package = "com.xquant";optionjava_outer_classname = "Order"; message Order { required int32 action = 1; required string serialNo = 2; required string version = 3; optional string operator = 4; required string code = 5; required string name = 6; required string price = 7; required string amount = 8;}
3、使用google的protoc.exe生成对应的C++文件,在CMD命令框中输入如下命令(路径根据实际情况修改):
F:\projects\c++\protobuf-2.4.1\examples>protoc--cpp_out=F:\projects\c++\protobu
f-2.4.1\examplesorder.proto
生成文件:
order.pb.h
order.pb.cc
4、新建C++工程Demo,把order.pb.h和order.pb.cc加入工程中,添加lib库
# pragma comment(lib,"libprotobuf.lib")
代码如下:
#include "stdafx.h"#include <iostream>#include <fstream>#include <string>#include "order.pb.h"using namespacestd; voidinitOrder(xquant::Order* order) { order->set_action(100); order->set_serialno("100abc"); order->set_version("1.00.003"); string code = "TF0001"; order->set_code(code); string name = "test"; order->set_name(name); order->set_price("10.01"); order->set_amount("10000000.00");} int_tmain(int argc, _TCHAR* argv[]){ GOOGLE_PROTOBUF_VERIFY_VERSION; // 组装报文 xquant::Order order; initOrder(&order); // 对象序列化为string string order_str; order.SerializeToString(&order_str); cout << order_str << endl; // 显示调式报文 string order_debug =order.DebugString(); cout << order_debug <<endl; // string反序列化为对象 xquant::Order order_2; order_2.ParseFromString(order_str); cout << order_2.code() << endl; cout << order_2.name() <<endl; google::protobuf::ShutdownProtobufLibrary(); getchar(); return 0;}
- Google ProtoBuf开发示例
- Google Protobuf开发指南
- JAVA操作google protobuf简单示例
- Windows下Google Protobuf Visual Studio自动编译脚本示例
- google protobuf
- Google ProtoBuf
- protobuf (GOOGLE)
- google protobuf
- google protobuf
- 【google protobuf】
- google protobuf
- Google protobuf
- Google protoBuf
- Google Protobuf
- Google protobuf
- google protobuf 学习
- google ProtoBuf开发者指南
- google protobuf 源码解析
- BCGControlBar的使用
- strncpy越界
- OralceTNS错误 (一)
- 《系统程序员成长计划》学习交流(2)-通用单链表
- PreTranslateMessage详解
- Google ProtoBuf开发示例
- QR码制作公司Paperlinks推订餐应用PayDragon
- Chap9:如何编写jni方法(转载)
- 关于触发器一点问题(事务处理中)
- 计算机浮点数 float 表示
- WinForm的listView多行删除操作
- 关键字 final 和static
- ubnutu添加删除PPA
- (精)hdoj 2309(贪心算法)(开始的时候我还怀疑是不是要多次循环安排,后来我发现第一次的最多)