Protobuf is simple to write and use

A brief introduction to Protobuf

First of all, Protobuf is a serialization method proposed by Google, because it has many advantages in network transmission, and it is simple to use and easy to maintain. Therefore, now Protobuf and gradually replace Json to transmit data.

Comparison of Protobuf and Json

  • Create a .proto file in which two message structures (student, teacher) are defined,
  • The corresponding classes of the message structure in C++ are: student, teacher

If we use Protobuf for transmission, we only need to assign a value to the class.

//name、age、gender分别为student类与teacher类的属性
student类:
name:'张三'
age:20
gender:'男'

teacher类:
name:'张老师'
age:35
gender:'男'

If we use Json for data transmission:
{"student":{"name":"张三","age":"20","gender":"男"},"teacher":{"name":"张老师","age":"35","gender":"男"})

Syntax of Protobuf

  • The first is to define the grammar format, which must be the first line, indicating that the following grammar uses proto3 grammar, if not written, proto2 grammar is used by default.
    syntax = "proto3"
  • Import other proto files
    import other.proto
  • Declare the scope, the method conflicts, similar to the namespace in C++
    package “xxx”
  • Define the message format
message Student{
    
    
	required string name = 1;
	required int32 age;
	required string gender;
}
  1. required: The modified field must be set
  2. optional: The modified field can have 0 or 1 value
  3. repeated: The modified field can be repeated any number of times
  4. During use, unless a certain field must be set, use optional or repeated instead
  5. message is similar to class in C++
//创建PersonMsg.proto文件
syntax = "proto3";//使用proto3语法格式
package PersonMsg;

message Person
{
    
    
	required string name = 1;
	required int32 age = 2;
	required string gender = 3;
	optional string email = 4;
	enum Profession //职业
	{
    
    
		Strudent = 1;
		Teacher = 2;
		Worker = 3;
	}
	enum PhoneType //电话卡类型
	{
    
    
		MOBILE = 0; //移动
		Link = 1;	//联通
	} 
	message PhoneNumber{
    
    
		requried string number = 1;
		optional PhonType P_type = 2 [default = Link]; //电话卡默认类型为联通 
	}
	repeated PhoneNumber number = 5; //可以重复多次
}

message PersonMessage
{
    
    
	repeated Person personInfo = 1;
}

Compiling the above proto file will generate two files PersonMsg.pb.h and PersonMsg.pb.cc

  • The message class is stored in the .h file
  • The .cc file stores some operations on the message, such as set, clear, has, etc.
  • Protobuf sequence and reverse sequence

Insert picture description here

#include<iostream>
#include"PersonMsg.pb.h"
using namespace std;

char buf[1024];
int main()
{
    
    
	PersonMsg::PersonMessage person;
	PersonMsg::Person* p = person.add_personInfo();
	p->set_name("张三");
	p->set_age(20);
	p->set_gender("男");
	if(!p->has_email())//如果获取不到email,表示没有email则对其进行初始化
	{
    
    
		p->set_email("[email protected]");
	}
	PersonMsg::Person::PhoneNumber* phonenum = person.add_number();
	phonenum->set_number("xxxxx");
	phonenum->set_P_type("PersonMsg::Person::Link");//设置电话
	int size = person.ByteSize();
	person.SerializeToArray(buf,size); //序列化

	PersonMsg::PersonMessage person2;
	person2.ParseFromArray(buf,size); //发序列化
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_42708024/article/details/112245206