Protobuf data serialization

1. Introduction to Protocol Buffer

Protocol buffer (hereinafter referred to as protocol buffer) is a mixed language data standard within Google. It is a lightweight and efficient structured data storage format, which can be used for structured data serialization. It is very suitable for data storage or RPC data exchange format.

Protobuf is a pure presentation layer protocol, which can be used with various transport layer protocols. Protobuf's documents are also very perfect. google provides implementations in many languages: java, c#, C + +, go and python. Each implementation includes the compiler and library files of the corresponding language.

Protobuf supports relatively few data types and does not support constant types. Because its design concept is pure presentation layer protocol, there is no RPC framework that specifically supports protobuf.

2. Protobuf usage process

2.1 data preparation

  • Composite type: structure / class
    • Data to serialize

    • Foundation type
// Data to serialize
struct Persion
    int id;
    string name;
    int age;

int id;

2.2. Create proto file, syntax format

// protobuf version
// proto2
syntax = "proto3";	
// Import another proto file
import "filename.proto";
// Organizational personality structure
// Syntax format
message keyword(Equivalent to the name of the class created)
	// Member variable
	Data type variable name = Number of variable;	// The number starts from 1 and cannot be repeated
  • repeated qualifier (using array)
  • Use import to import another proto file
  • Adding namespaces using package
  • Examples
// Persion.proto
syntax = "proto3";
// Import another proto file
import "Info.proto";
// Add namespace
package namespace;	// The person class belongs to the namespace
enum TypeValue
	value1 = 0;	// The first enumeration value in protbuf must be 0
	value2 = 6;
	value3 = 9;

message Persion
    int32 id = 1;   // Number starts from 1
    repeated bytes name = 2;  //repeated modified name can create multiple in the program and be used as a dynamic array in the program
    int32 age = 3;
    TypeValue value = 4;
    Info info = 5;	// Info object, class in imported proto file
// Info.proto
syntax = "proto3";
// Add namespace
package namespace;	// The person class belongs to the namespace
message Info
    bytes address = 1; 
    int32 number = 2;   
  •  . proto data type
. proto typeC + + typeremarks
doubledouble64 bit floating point number
floatfloat32-bit floating point number
int32int3232-bit integer
int64int6464 bit integer
uint32uint3232-bit unsigned integer
uint64uint6464 bit unsigned integer
sint32sint3232-bit integer, processing negative numbers is more efficient than int32
sint64sint6464 bit integer, processing negative numbers is more efficient than int64
fixed32uint32Always 4 bytes. If the value is always greater than 228, this type will be more efficient than uint32.
fixed64uint64Always 8 bytes. If the value is always greater than 256, this type will be more efficient than uint64.
sfixed32int32Always 4 bytes
sfixed64int64Always 8 bytes
boolboolBoolean type
stringstringA string must be UTF-8 encoded or 7-bit ASCII encoded text
bytesstringHandle multi byte language characters, such as Chinese
messageobject of classCustom message type

2.2. Use protobuf compiler to generate C + + classes

# protobuf compiler, compile the source code to get the protoc exe
# grammar
# --cpp_out the location of the generated c + + class

protoc.exe xxx.proto --cpp_out=catalogue

2.3. Use C++ API to read and write messages

  • Read: variable name ()

  • Write: set_ Variable name (arg1, arg2,...)

  • Some C + + interface functions

Person p;
//Serialize output as an outgoing parameter
p.SerializeToString(std::string* output);

Person pp;

2.4 use in VS

  • Add the generated C + + classes and header files to the project
  • Reference in header file in project file
  • Note: for VS configuration, the macro "PROTOBUF_USE_DLLS" needs to be added in C/C + + - "preprocessor -"

Tags: C++

Posted by laPistola on Thu, 14 Apr 2022 20:55:29 +0930