01 | profile

Protobuf (Protocol Buffers) is a cross-language and cross-platform extensible mechanism developed by Google for serializing structured data.

Protobuf is smaller, faster, and more convenient than XML and JSON formats. Protobuf currently supports C++, Java, Python, Objective-C, and C#, Ruby, Go, PHP, and JavaScript if proto3 is used.

Website address: developers. Google. Cn/protocol – bu…

GitHub address: github.com/protocolbuf…

Advantages:

  • Performance is good
  • cross-language

Disadvantages:

  • Poor readability in binary format: Protobuf is encoded in binary format to improve performance, which directly results in poor readability.
  • Lack of self-description: XML is self-describing, protobuf is not, and structures that do not match the definition are not visible.

02 | install

2.1 Installation on Windows

Download: github.com/protocolbuf…

Download protoc-3.9.1-win64.zip, this is a compiled compression package, equivalent to the green version, after decompression, add the bin directory under it to the environment variable can, save the trouble of installation.

Then open a command prompt and type the command:

protoc --version
Copy the code

If the version number is displayed, the installation is successful. The diagram below:

03 | is simple to use

3.1 build

With protobuf you first need to define a.proto file. Let’s start with a simple example.

Define the person. proto file as follows:

syntax = "proto3";
package Test;

message Person {
  string Name = 1;
  int32 Age = 2;
  bool Marriage = 3;
}
Copy the code
  • syntax = "proto3";Specifies that proto3 syntax is being used, otherwise protobuf will default to Proto2.
  • package Test;Specify a namespace (in C#).
  • messageIs the keyword that defines structured data.
  • The number following the equal sign is the unique field number (note not the value of the field) and is used to identify the field in a binary format message.

Protoc is a built-in compiler for protobuf, which can compile.proto files into Java, python, go, C# and other languages.

Compile command:

protoc -I=E:\GL\Test2017 --python_out=E:\GL\Test2017 Person.proto
Copy the code

Compile command description:

  • -i indicates the directory where the source file (. Proto file) resides.
  • –python_out indicates that the target language is Python and specifies the directory to store the generated.py file. Accordingly, C# is csharp_out,
  • Person.proto is the name of the source file. If there are more than one, separate them with Spaces.

3.2 the Python example

Protobuf installation.

Call the compile command to compile person. proto, generate file: person_pb2.py, add to the project, serialization and deserialization examples are as follows:

import Person_pb2

person = Person_pb2.Person()
person.Name = 'Joe'
person.Age = 20
person.Marriage = True

# serialization
b = person.SerializeToString()
print(b)

# deserialize
p = Person_pb2.Person()
p.ParseFromString(b)
print(f'Name: {p.Name}; Age: {p.Age}; Marriage: {p.Marriage}')
Copy the code

Output:

b'\n\x06\xe5\xbc\xa0\xe4\xb8\x89\x10\x14\x18\x01'Name: zhang SAN; Age: 20; Marriage: TrueCopy the code

Note that you can’t write this, it’s wrong:

p = Person_pb2.Person().ParseFromString(b)
Copy the code

3.3 c # example

There are 3 versions of Protobuf under C# :

  • Google.ProtoBuf: official version of Google, github.com/google/prot…
  • Protobuf-net:.net community version, developed by.net community enthusiasts, github.com/mgravell/pr…
  • Google.protocolbuffers: Allegedly developed by Google.NET staff before the official version was available, github.com/jskeet/prot…

Here we present the official Google version.

In VS, install the ‘Google.protobuf’ package via NuGet.

using Google.Protobuf;
using System;
using Test;

namespace Protobuf
{
    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person();
            person.Name = "Zhang";
            person.Age = 20;
            person.Marriage = true; // serialize byte[] buffer = person.tobytearray (); foreach (byte bin buffer)
            {
                Console.Write(b.ToString("X2") + ""); } Console.WriteLine(); // deserialize Person p = person.parsefrom (buffer); Console.WriteLine(string.Format("Name: {0}, Age: {1}, Marriage: {2}", p.Name, p.Age, p.Marriage)); Console.Read(); }}}Copy the code

Output:

0A 06 E5 BC A0 E4 B8 89 10 14 18 01 Name: zhang Three, Age: 20, Marriage: TrueCopy the code

In Python, the first byte is \n, whereas here it is 0A. The value of \n in ASCII is 0A. So the serialization results are the same in both languages.