ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

.NET Protobuf包装器库

2021-11-17 15:35:33  阅读:131  来源: 互联网

标签:RepeatedField delimited Protobuf 器库 Length NET 序列化 Message


Wodsoft Protobuf Wrapper

内容

关于

这是一个可以帮助你不需要.proto文件就能够使用Protobuf序列化的一个库。

通常.proto文件会创建继承IMessage接口的模型,Protobuf使用这些模型来进行序列化。

有时候我们已经在自己的.NET项目里创建了一些模型,但我们需要使用Protobuf对这些模型进行序列化。
这时候这个库就能帮助你使用Protobuf对已存在的模型进行序列化。

Github地址:Wodsoft.Protobuf.Wrapper

需求

Wodsoft.Protobuf.Wrapper需要NETStandard 2.0或以上。

这个库需要工作在允许动态代码编译的平台。所以IOS不支持

安装

在NuGet上获取Wodsoft.Protobuf.Wrapper.

dotnet add package Wodsoft.Protobuf.Wrapper

用法

序列化

可以使用Wodsoft.Protobuf.Message类中的静态方法Serialize
你需要一个System.IO.Stream来存储序列化后的数据。

YourModel model = new ();
MemoryStream stream = new MemoryStream();
Message.Serialize(stream, model);

这里也有一个重载方法。
你可以传递一个Google.Protobuf.CodedInputStream来替代System.IO.Stream

YourModel model = new ();
CodedInputStream input = ...;
Message.Serialize(input, model);

或者你想直接拿到序列化后的字节数组。

YourModel model = new ();
var bytes = Message.SerializeToBytes(model);

反序列化

你可以使用Wodsoft.Protobuf.Message类中的静态方法Deserialize
你需要传递包含需要反序列化数据的System.IO.Stream
它将返回你的泛型对象T

Stream stream = ...;
YourType model = Message.Deserialize<YourType>(stream);

这里也有一个重载方法。
你可以传递一个Google.Protobuf.CodedOutputStream来替代System.IO.Stream

CodedOutputStream output = ...;
YourType model = Message.Deserialize<YourType>(output);

或者你想直接从字节数组进行反序列化。

YourType model = Message.DeserializeFromBytes<YourType>(bytes);

字段定义

IMessageFieldProvider.GetFields(Type type)会返回从对象映射而来的消息字段。

默认实现是GeneralMessageFieldProvider.Intance类。
它只会映射可读写的属性到消息字段。

你可以创建自己的IMessageFieldProvider去映射消息字段。
然后通过设置静态属性Message<T>.FieldProvider为自定义的IMessageFieldProvider

你需要为每个需要自定义消息字段的类型设置IMessageFieldProvider

字段排序

给属性添加System.Runtime.Serialization.DataMemberAttribute特性然后设置Order属性。
不然将根据属性名称进行排序。

⚠️ 如果有任何一个属性使用了DataMemberAttribute特性,将只会序列化拥有DataMemberAttribute特性的属性。

⚠️ 如果全部没有使用DataMemberAttribute特性,服务如果因为部署问题使用了不同版本的模型,反序列化时可能因为字段排序问题存在错误。

非空构造函数

通过调用静态方法MessageBuilder.SetTypeInitializer<T>(Func<T> initializer)来设置对象初始化委托。

获取Protobuf包装器

我们可以直接转换模型对象为Message<>

SimplyModel model;
Message<SimplyModel> message = model;

然后这个message可以直接被Protobuf序列化。

高级

支持的属性类型与Protobuf类型的关系

C#类型 Protobuf类型 消息结构
bool(?) bool Varint
sbyte(?) int32 Varint
byte(?) int32 Varint
short(?) int32 Varint
ushort(?) int32 Varint
int(?) int32 Varint
long(?) int64 Varint
uint(?) uint32 Varint
ulong(?) uint64 Varint
float(?) float Varint
double(?) double Varint
string string Length-delimited
byte[] ByteString Length-delimited
Guid(?) ByteString Length-delimited
DateTime(?) google.protobuf.Timestamp Length-delimited
DateTimeOffset(?) google.protobuf.Timestamp Length-delimited
TimeSpan(?) google.protobuf.Duration Length-delimited
IMessage Length-delimited
T[] RepeatedField<T> Length-delimited
ICollection<T> RepeatedField<T> Length-delimited
Collection<T> RepeatedField<T> Length-delimited
IList<T> RepeatedField<T> Length-delimited
List<T> RepeatedField<T> Length-delimited
IDictionary<TKey, TValue> MapField<TKey, TValue> Length-delimited
Dictionary<TKey, TValue> MapField<TKey, TValue> Length-delimited
  • (?) 意思是可以为Nullable<>可空类型。
  • 可以直接使用继承了Google.Protobuf.IMessage的Protobuf对象作为属性类型。
  • 所有RepeatedFieldMapField对象不能包含null值。
  • 支持bytesbyteshortushort作为属性类型。
    它们将作为int类型进行序列化。
    如果从其它第三方来源数据进行反序列化,int可能会丢失数据。

如何工作

首先,Protobuf通过Google.Protobuf.IMessageGoogle.Protobuf.IBufferMessage接口进行序列化工作。

我们定义了一个抽象类Wodsoft.Protobuf.Message
然后定义抽象保护方法ReadWriteCalculateSize
显式实现这些接口并调用这些方法。

然后定义泛型抽象类Wodsoft.Protobuf.Message<T>
这里有一个属性可以直接获取到原始类型值。然后我们实现了一些隐式转换操作。

public T Source { get; }

最后,为需要序列化的类型动态创建继承了Message<T>的类。
通过Emit动态创建代码实现ReadWriteCalculateSize方法。

性能

  • 建议使用 RepeatedField<>IList<>ICollection<>作为集合属性的类型。
    使用RepeatedField<>会获得最佳性能(因为不需要额外类型转换)。
  • 使用IList<>ICollection<>在序列化时会转换为RepeatedField<>
  • 使用List<>Collection<>在序列化时会转换为RepeatedField<>
    并且在反序列化时会转换回List<>Collection<>(上一个会直接返回RepeatedField<>)。
  • 推荐使用 MapField<,>IDictionary<,>作为字典属性的类型。
    使用MapField<,>会获得最佳性能
  • 使用IDictionary<,>在序列化时会转换为MapField<,>
  • 使用Dictionary<,>在序列化时会转换为MapField<,>
    并且在反序列化时会转换回Dictionary<,>(上一个会直接返回MapField<,>)。

许可证

库使用MIT许可证。

标签:RepeatedField,delimited,Protobuf,器库,Length,NET,序列化,Message
来源: https://www.cnblogs.com/Kation/p/15567506.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有