ProtoBuf序列化和反序列化方法

using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProtoBuf.Serializers;
namespace Prorobuf_net_ProtoContract_
{
    public class ProtoBuf_Serialize
    {
        static void Main()
        {
            ErrorLog errorLog = new ErrorLog();
            errorLog.LogID = "1234";
            Byte[] bytes = Serialize(errorLog);
            string sa = Encoding.UTF8.GetString(bytes);
            Console.WriteLine(sa);
        }
        /// <summary>
        /// 将消息序列化为二进制的方法
        /// </summary>
        /// <param name="log">要序列化的对象</param>
        /// <returns></returns>
        public static byte[] Serialize(ErrorLog log)
        {
           // 涉及格式转换,需要用到流,将二进制序列化到流中
            using (MemoryStream memoryStream = new MemoryStream())
            {
                //使用ProtoBuf工具的序列化方法
                ProtoBuf.Serializer.Serialize(memoryStream, log);
                return memoryStream.ToArray();
            }
        }
        /// <summary>
        /// 将收到的消息反序列化成对象
        /// </summary>
        /// <param name="data">收到的消息</param>
        /// <returns></returns>
        public static ErrorLog DeSerialize(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                //使用工具反序列化对象
                return ProtoBuf.Serializer.Deserialize<ErrorLog>(ms);
            }
        }
    }

    [ProtoContract]
    public class ErrorLog
    {
        [ProtoMember(1)]
        public string LogID { get; set; }
        [ProtoMember(2)]
        public string Context { get; set; }
        [ProtoMember(3)]
        public string Stack { get; set; }
    }
}

辅助:Protobuf-net的使用 - 知乎 (zhihu.com)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ProtobufProtocol Buffers)是一种由Google开发的高效的序列化反序列化技术。它可以将结构化数据转换为紧凑且高效的二进制格式,以便在不同的系统之间进行数据交换和存储。下面是Protobuf序列化反序列化的介绍和示例: 1. 定义消息结构:首先,我们需要定义消息的结构,即使用Protobuf的语法来定义消息的字段和类型。例如,我们可以定义一个名为Person的消息,其中包含姓名和年龄两个字段: ```protobuf syntax = "proto3"; message Person { string name = 1; int32 age = 2; } ``` 2. 编译消息定义:接下来,我们需要使用Protobuf编译器将消息定义编译成相应的代码文件。例如,使用protoc命令将上述消息定义编译成Python代码: ```shell protoc --python_out=. person.proto ``` 3. 序列化:在发送方,我们可以使用生成的代码将消息对象序列化为二进制数据。例如,在Python中,我们可以使用生成的代码创建Person对象,并将其序列化为字节串: ```python from person_pb2 import Person person = Person() person.name = "Alice" person.age = 25 serialized_data = person.SerializeToString() ``` 4. 反序列化:在接收方,我们可以使用生成的代码将接收到的二进制数据反序列化为消息对象。例如,在Python中,我们可以使用生成的代码将字节串反序列化为Person对象: ```python from person_pb2 import Person received_data = b'\n\x05Alice\x10\x19' person = Person() person.ParseFromString(received_data) print(person.name) # 输出:Alice print(person.age) # 输出:25 ``` 通过使用Protobuf进行序列化反序列化,我们可以实现高效的数据交换和存储,同时减少网络传输和磁盘空间的占用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DAGUNIANGZHOU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值