protobuf使用

XML、JSON、ProtoBuf 都具有数据结构化和数据序列化的能力
XML、JSON 更注重数据结构化,关注人类可读性和语义表达能力。ProtoBuf 更注重数据序列化,关注效率、空间、速度,人类可读性差,语义表达能力不足(为保证极致的效率,会舍弃一部分元信息)
ProtoBuf 的应用场景更为明确,XML、JSON 的应用场景更为丰富。

linux下安装protobuf包
sudo ./configure
sudo make && make install
make编译时间有点长

Person.pro

syntax = "proto2"; //不添加会报警告错误
package demo;

message Person
{
	//字段规则 类型 名称 编号
	
	//required修饰字段 只能也必须出现1次
	//repeated 字段可出现多次,包括0次
	//optional 字段出现0次或1次
	//类型:int32,int64,sint32,sint63,string, 32-bit...
    required string name=1;			
    required int32 age=2;		
    optional string email=3;
}

在.profile中添加环境变量

#编译Person.pro
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
protoc --cpp_out=./ Person.pro
#生成如下几个文件
1.Person.pro.pb.h
2.Person.pro.pb.cc

Person.cc源文件demo

#include "Person.pro.pb.h"
#include <iostream>

using namespace std;

int main()
{
    demo::Person per;
    per.set_name("zhangsan");
    per.set_age(20);
    per.set_email("1549189372@qq.com");
    
    //将消息格式化到字符串中
    string in_data;
    
    per.SerializeToString(&in_data);
    cout<<"in_data format:"<<in_data<<endl;
    
    //从字符串in_data进行解析
    demo::Person decode_msg;
    decode_msg.ParseFromString(in_data);
    cout<<"name:"<<decode_msg.name()<<endl;
    cout<<"age:"<<decode_msg.age()<<endl;
    if (decode_msg.has_email())
    {
        cout<<"email:"<<decode_msg.email()<<endl;
    }
    

}

#使用如下命令对源文件进行编译
g++ Person.cc Person.pro.pb.cc -o Person -lprotobuf

报如下错误信息:
dard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support
^
In file included from /usr/local/include/google/protobuf/stubs/common.h:52:0,
from Person.pro.pb.h:9,
from Person.cc:1:
/usr/local/include/google/protobuf/stubs/mutex.h:58:8: error: ‘mutex’ in namespace ‘std’ does not name a type
std::mutex mu_;
提示必须加上-std=c++11 or -std=gnu++11

g++ Person.cc Person.pro.pb.cc -o Person -std=c++11 -lprotobuf

报如下错误:
terminate called after throwing an instance of ‘std::system_error’
what(): Unknown error -1
Aborted (core dumped)
需要加上-lpthread

g++ Person.cc Person.pro.pb.cc -o Person -std=c++11 -lprotobuf

生成结果如下
在这里插入图片描述

ProtobufProtocol Buffers)是一种轻量级的数据序列化格式,由Google开发。它可以用于结构化数据的序列化,用于数据通信、持久化和配置文件等场景。下面是使用protobuf的一般步骤: 1. 定义消息类型:使用protobuf语言定义文件(.proto)来描述数据结构和消息类型。你可以定义消息字段的名称、类型和规则等。 2. 编写.proto文件:在.proto文件中定义消息类型、字段和其他相关信息。例如,你可以定义消息的名称、字段的名称和类型、字段的规则(如必填、可选或重复)等。 3. 编译.proto文件:使用protobuf编译器将.proto文件编译为你所选编程语言的源代码。protobuf支持多种编程语言,如C++、Java、Python等。编译后会生成对应语言的源代码文件,其中包含与消息类型相关的类或结构体。 4. 在代码中使用protobuf:在你的代码中引入生成的源代码文件,并使用其中定义的类或结构体。你可以根据需要创建、修改和序列化protobuf消息,以及将其转换为二进制格式或其他格式。 5. 序列化和反序列化:使用protobuf库提供的方法将protobuf消息序列化为二进制格式,或者将二进制数据反序列化为protobuf消息。这样可以实现消息的传输和存储。 总结来说,使用protobuf可以实现跨语言、高效的数据序列化和反序列化,简化了数据传输和存储的过程。通过定义和编译.proto文件,并在代码中使用生成的源代码文件,你可以方便地使用protobuf进行数据处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值