简体   繁体   English

protobuf-net文档或替代方案

[英]protobuf-net documentation or alternatives

Protobuf-net seems to be the fastest and for high-performence needs most recommended serialization library for .NET. Protobuf-net似乎是最快的,并且对于高性能需要最推荐的.NET序列化库。 I really want to use it, as I need to send hundreds of thousands of objects over the wire. 我真的很想使用它,因为我需要通过线路发送数十万个对象。

However, I'm having trouble getting started. 但是,我开始时遇到了麻烦。 The documentation (wiki at github) is quite sparse, especially for v2. 文档(github上的wiki)非常稀疏,特别是对于v2。

Somehow, you guys out there seem to be able to get going with the lib. 不知怎的,你们那里似乎能够开始使用lib。 How? 怎么样? By reading the sources? 通过阅读消息来源? Trial and error? 试错了? Or are there some API-docs / tutorials I'm not aware of? 或者是否有一些我不知道的API文档/教程? (I'm only aware of the GitHib page.) (我只知道GitHib页面。)

Thanks and cheers, 谢谢和欢呼,

Jan 一月

PS: I need to get going using the RuntimeTypeModel (POCO's without attributes). PS:我需要使用RuntimeTypeModel(没有属性的POCO)。

Since you've also asked about alternatives... 既然你也问过替代品......

No need for attribute decoration was one of the reasons behind creating Migrant , fast serialization library with simple API. 不需要属性修饰是创建具有简单API的Migrant ,快速序列化库的原因之一。 The library has some ideas that are also present in protobuf (so we are more or less on par in terms of speed and size), but at the same time tries to solve different problems. 图书馆有一些想法也存在于protobuf中(所以我们或多或少在速度和大小方面都相同),但同时试图解决不同的问题。 Among features distinctive from protobuf, there is a difference between empty and null collection and whole serialization is reference based for references and value based for values (well, you can also treat reference as a special kind of value). 在与protobuf不同的特征中,空集合和空集合之间存在差异,整个序列化是基于引用的参考和基于值的值(嗯,您也可以将引用视为一种特殊的值)。 README at github should be able to answer most of your questions; github上的README应该能够回答你的大部分问题; whether some more detailed info is needed, just ask. 是否需要更详细的信息,请问。

Simple scenario of custom object serialization: 自定义对象序列化的简单方案:

var stream = new MyCustomStream();
var myComplexObject = new MyComplexType(complexParameters);
var serializer = new Serializer();

serializer.Serialize(myComplexObject, stream);

stream.Seek(0, SeekOrigin.Begin);

var myDeserializedObject = serializer.Deserialize<MyComplexType>(stream);

Note that expected type in Deserialize is only used to have nice compile-time type of the deserialized object, you can use general object as well. 请注意, Deserialize中的预期类型仅用于具有反序列化对象的良好编译时类型,您也可以使用常规object

Disclaimer: I'm one of the developers. 免责声明:我是开发人员之一。

In protobuf, each member of a type needs an identifying number, because protobuf is number-based (it doesn't send names). 在protobuf中,每个类型的成员都需要一个识别号码,因为protobuf是基于数字的(它不发送名称)。 As such, the trick is simply: tell it what numbers to use. 因此,诀窍很简单:告诉它使用什么数字。 For example: 例如:

class Customer {
    public int Id {get;set;}
    public string Name {get;set;}
}

The simplest way to specify a contract for this would be: 为此指定合同的最简单方法是:

RuntimeTypeModel.Default.Add(typeof(Customer), false).Add("Id", "Name");

which would associate Id with 1 and Name with 2. When using attributes, there is some inbuilt "figure it out yourself" code, which I should really expose on the non-attribute API - things like: Id与1和Name关联起来2.当使用属性时,有一些内置的“自己搞定”代码,我应该在非属性API上公开 - 例如:

  • serialize all public fields + properties, in alphabetical order 按字母顺序序列化所有公共字段+属性
  • serialize all fields (public or non-public), in alphabetical order 按字母顺序序列化所有字段(公共或非公共字段)

However: both of these are pretty simple to do with reflection. 但是:这两个都很容易用反射做。 Note that in either event, using reflection is a good way to run into problems if the types might change at some point. 请注意,在任何一种情况下,如果类型可能在某些时候发生变化,使用反射是遇到问题的好方法。

Additional features that may help, and which I can provide more information on: 其他可能有用的功能,我可以提供更多信息:

  • a type factory can be specified (globally or per-instance), which can be useful for pre-populating values or using a pool of available objects 可以指定类型工厂(全局或每个实例),这对于预填充值或使用可用对象池非常有用
  • surrogate types can be written for complex scenarios - this is useful when most of the model works fine, but one type is just too esoteric to be a good fit for serialization - but an alternative layout can be devised for which you can write conversion code in both directions 可以为复杂场景编写代理类型 - 这在大多数模型工作正常时非常有用,但是一种类型太深奥以至于不适合序列化 - 但可以设计一种替代布局,您可以在其中编写转换代码两个方向
  • many things that looks like a "tuple" will be handles by default - in particular, if it is publicly immutable, and has a constructor that accepts parameters that match all the public members - it will assume to serialize the members in the order specified by the constructor 许多看起来像“元组”的东西默认是句柄 - 特别是,如果它是公共不可变的,并且有一个接受与所有公共成员匹配的参数的构造函数 - 它将假定按照指定的顺序序列化成员。构造函数

Another option is Dasher (available via NuGet ). 另一种选择是Dasher (可通过NuGet获得 )。

It's a straightforward, fast and lightweight serialiser and deserialiser for .NET that works on C# types without needing any attributes or other decorations. 它是一个简单,快速,轻量级的.NET序列化器和解串器,适用于C#类型,无需任何属性或其他装饰。

var stream = new MemoryStream();

// serialise to stream
new Serialiser<Holiday>().Serialise(stream, christmas);

stream.Position = 0;

// deserialise from stream
var christmas = new Deserialiser<Holiday>().Deserialise(stream);

It uses reflection emit to produce highly optimised serialisation and deserialisation functions at runtime. 它使用反射发射在运行时生成高度优化的序列化和反序列化功能。 The underlying encoding is MsgPack which is self describing, so slightly larger than protobuf on the wire, but it does mean you can decode any message you receive including property names, types and values. 底层编码是MsgPack,它是自描述的,因此稍微大于线上的protobuf,但它确实意味着您可以解码您收到的任何消息,包括属性名称,类型和值。

Disclaimer: I wrote the library. 免责声明:我写了这个库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM