"Dotnet9" series -Google ProtoBuf simple application in C #

Time is like water, you can not go back into the flow!

Thumbs up again, a habit, which is that you give me creative power!

This article Dotnet9 https://dotnet9.com already been included, the owners are happy to share dotnet related technologies, such as Winform, WPF, ASP.NET Core, etc., are also related to the desktop C ++ Qt Quick and Qt Widgets, just share their own familiar, own will.

Brief introduction

What is Google Protocol Buffer? If you search online, you should get such a text similar to the description:

Google Protocol Buffer (referred Protobuf) is a mixed-language Google's internal data standards, there are already being used by more than 48,162 kinds of message format definition and a .proto more than 12,183 files. They continued for RPC systems and data storage systems.

Protocol Buffers is a lightweight, highly efficient data storage structure format, the structure may be used for data serialization, or serialization. It is suitable for data storage or RPC data exchange format. Protocol languages ​​may be used, regardless of the data storage fields, platform-independent, scalable serial data format structure. Currently provides C ++, Java, Python three languages ​​API.

Said 1000 10000, Google Protocol Buffer is a serialized data structure is a good helper, with respect to XML, binary serialization, Protobuf high efficiency, support the large amount of data, the size of the protobuf json serialization is 1 / 10, xml format 1/20, 1/10 of a binary sequence (specifically, I have not tested, whereby data obtained articles: Protobuf efficiency )

C #, how to use?

First, the preparatory work

  1. Visual Studio 2019 (other versions may also be)
  2. Nuget installation: Google.Protobuf and Google.Protobuf.Tools
  3. Document preparation .proto

Second, on the proto file

Use ProtoBuf, there are two operations: serialization and deserialization. Both of these operations require protocol description file, which is .proto file. If you want to use custom data protobuf storage, it is necessary to write your own proto file, if you want to read other ProtoBuf serialized file, we must first know the protocol ProtoBuf serialized file to be read, that is, to obtain the corresponding. proto file, which is a necessary condition, there is no corresponding proto file, you can not open ProtoBuf serialized file correctly.

Third, step

1, the installation Google.Protobuf and Google.Protobuf.Tools packet Nuget

"Dotnet9" series -Google ProtoBuf simple application in C #

2, find a compilation tools protoc.exe in Google.Protobuf.Tools, my computer path is: C: \ Users \ admin.nuget \ packages \ google.protobuf.tools \ 3.10.1 \ tools \ windows_x64, in this on the directory, there are many versions, see your specific version of the program and tools on the version.

3, ready agreement profile xx.proto, note that you can refer to each other between the proto file, to normal use, you must put all the proto-related documents are ready. Here is my own to write a test file test.proto:

syntax = "proto3";
option cc_enable_arenas = true;

package Test;

message TestContact {
    int32 ID = 1;
    string Address = 2;
    string Name = 3;
}

4, the decoder generates

  1. Create two folders named src, the other is gen
  2. The prepared all the documents in the src proto, as my test.proto
  3. Run the command:. \ Protoc.exe -proto_path = src -csharp_out = gen test.proto
  4. All the proto files are generated again
  5. In gen folder, you will find the same amount of .cs file, which is the corresponding decoder, we take them into his project.

"Dotnet9" series -Google ProtoBuf simple application in C #"Dotnet9" series -Google ProtoBuf simple application in C #"Dotnet9" series -Google ProtoBuf simple application in C #"Dotnet9" series -Google ProtoBuf simple application in C #"Dotnet9" series -Google ProtoBuf simple application in C #

5, the mounting opening and Google.Protobuf.Tools C # Google.Protobuf engineering of Nuget packet, the decoder generates just introduced works.

"Dotnet9" series -Google ProtoBuf simple application in C #I put the code into the structural engineering with tools tools and documents and proto proto cs file

6, the specific use of the code

using Google.Protobuf;
using System;
using System.IO;
using Test;

namespace protoctest
{
    class Program
    {
        static void Main(string[] args)
        {
            TestContact t = new TestContact();
            t.ID = 1;
            t.Name = "xiao ming";
            t.Address = "Cheng Du";
            Console.WriteLine($"序列化之前:{t}");

            //序列化操作
            byte[] data = new byte[t.CalculateSize()];
            using (CodedOutputStream cos = new CodedOutputStream(data))
            {
                t.WriteTo(cos);
                //data = cos.to.ToArray();
            }

            //反序列化操作
            TestContact t1 = TestContact.Parser.ParseFrom(data);
            Console.WriteLine($"反序列化得到:{t1}");

            Console.ReadKey();
        }
    }
}

7、运行效果如下

"Dotnet9" series -Google ProtoBuf simple application in C #

四、代码

代码已上传CSDN:C#使用Google ProtoBuf例子

gitee: https://gitee.com/lsq6/GoogleProtoCSharpTest

Reference article as follows:

  1. Protocol Buffer Basics: C#
  2. C #, using Protocol Buffers agreement to serialize and deserialize objects
  3. Use Google ProtoBuf in C #

Except where noted, the article by  Dotnet9  finishing release, welcome to reprint.

Address reprint this article please specify: https://dotnet9.com/2019/12/it-technology/csharp/dotnet9-series-google-protobuf-in-csharp.html

If the harvest, please forward vigorously (thumbs and can recommend it is excellent); as written text is not easy to feel small, welcome to the site Dotnet9 a reward , small thank you; thank you for concern and support for dotnet technology.

Guess you like

Origin www.cnblogs.com/Dotnet9-com/p/12052131.html