当前位置:网站首页>Protobuf binary file learning and parsing
Protobuf binary file learning and parsing
2022-06-29 07:56:00 【Hermit_ Rabbit】
0. brief introduction
protobuf Also called protocol buffer yes google A data exchange format of , It is independent of language , Platform independent .google Provides the implementation of multiple languages :java、c#、c++、go and python, Each implementation contains the compiler and library files of the corresponding language .
Because it's a binary format , Than using xml 、json Data exchange is much faster . It can be used for data communication between distributed applications or data exchange in heterogeneous environment . As a binary data transmission format with excellent efficiency and compatibility , Can be used for such as network transmission 、 The configuration file 、 Data storage and many other fields .
For more details, please see :https://developers.google.com/protocol-buffers/docs/overview
1. Basic grammar
1 Field restrictions
required: Characters that must be assigned optional: Optional fields , have access to [default = xxx] Configure defaults repeated: Repeatable variable length field , It's like an array
2 tag
Each field has a unique tag
tag 1-15 Is byte encoding ,16-2047 Use 2 Byte encoding , therefore 1-15 For frequently used fields
3 type

System default :
string The default is an empty string ;
bool The default is false;
The default value is 0;
enum The default is the first element
4 Parsing and serialization
Every message Both contain the following methods , For parsing and serializing , Note that the target is in byte form , Non text .bool SerializeToString(string* output) const: take message Serialization into binary is stored in output in , Note that it is binary , It's not text ; just string As a container .bool ParseFromString(const string& data): From the given binary value, it can be resolved to messagebool SerializeToOstream(ostream* output) const: Serialize to ostream in bool ParseFromIstream(istream* input): from istream Resolve in message
3. Explanation examples
establish .proto file
syntax = "proto3";// Specify version information , No error will be reported
message Person //message Keyword , The function is to define a message type
{
string name = 1; // full name
int32 id = 2; //id
string email = 3; // mail
}
message AddressBook
{
repeated Person people = 1;
}
Field API
Generally, we often use protoc To automatically generate :
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/addressbook.proto
among protoc Tools to address :protoc
The field modifier is repeated Function generated by the field of , It's a little different , Such as people Field , Then the compiler will generate the following code :
int people_size() const;
void clear_people();
const ::Person& people(int index) const;
::Person* mutable_people(int index);
::Person* add_people();
::google::protobuf::RepeatedPtrField< ::Person >* mutable_people();
const ::google::protobuf::RepeatedPtrField< ::Person >& people() const;
The test program
void set_addressbook()
{
AddressBook obj;
Person *p1 = obj.add_people(); // Add a new Person
p1->set_name("mike");
p1->set_id(1);
p1->set_email("[email protected]");
Person *p2 = obj.add_people(); // Add a new Person
p2->set_name("jiang");
p2->set_id(2);
p2->set_email("[email protected]");
Person *p3 = obj.add_people(); // Add a new Person
p3->set_name("abc");
p3->set_id(3);
p3->set_email("[email protected]");
fstream output("pb.xxx", ios::out | ios::trunc | ios::binary);
bool flag = obj.SerializeToOstream(&output);// serialize
if (!flag)
{
cerr << "Failed to write file." << endl;
return;
}
output.close();// Close file
}
void get_addressbook()
{
AddressBook obj;
fstream input("./pb.xxx", ios::in | ios::binary);
obj.ParseFromIstream(&input); // Deserialization
input.close(); // Close file
for (int i = 0; i < obj.people_size(); i++)
{
const Person& person = obj.people(i);// Take the first place i individual people
cout << " The first " << i + 1 << " Messages \n";
cout << "name = " << person.name() << endl;
cout << "id = " << person.id() << endl;
cout << "email = " << person.email() << endl << endl;
}
}
Running results :

… Please refer to Ancient Moon House
边栏推荐
- MIPS instruction set and brief analysis
- SizeBalanceTree
- TF. Repeat and stack operations of slim
- 基础知识 - 语法标准(ANSI C、ISO C、GNU C)
- Cartographer中的线程池操作
- 面试官:为什么数据库连接很消耗资源,资源都消耗在哪里?
- nor flash 应用层操作
- IndexTree以及应用
- Roblox sword nine sword two
- AI and the meta universe sparked a spark: human beings lost only shackles and gained all-round liberation
猜你喜欢

code::blocks代码格式化快捷键

Vulnhub's dc6 target

Viewing application and installation of Hana database license

Line features & surface features of vSLAM features

AI and the meta universe sparked a spark: human beings lost only shackles and gained all-round liberation

电检码配置

AI与元宇宙擦出火花:人类失去的只有枷锁,获得的是全方面的解放

Problem solving -- > online OJ (13)

VMware vcenter/ESXI系列漏洞总结
![Detailed explanation of communication principle between [industrial control old horse] single chip microcomputer and Siemens S7-200](/img/56/b300c0c3606dbc328e301092615bff.jpg)
Detailed explanation of communication principle between [industrial control old horse] single chip microcomputer and Siemens S7-200
随机推荐
[industrial control old horse] detailed design of PLC six way responder system
互联网公司的组织结构与产品经理岗位职责是什么?
编程能力提升方向
多态中的向上和向下转型
Software testing
Vulnhub's dc6 target
MIPS instruction set and brief analysis
Cartographer中的线程池操作
code::blocks代码格式化快捷键
101. 对称二叉树(递归与迭代方法)
VMware vcenter/ESXI系列漏洞总结
Embedded product anti-theft version
Time operation - time format conversion
Vulnhub's dc7 target
JS to implement a detailed scheme for lazy loading of pictures (it can be used after being imported)
Environmental preparation - Engineering Management
Reasons why the ext.dic file configured in ES does not take effect
C # import CSV into MySQL database
C#Mqtt订阅消息
Handle series - install spotbugs and use them quickly