protobuf-net,它是怎么做的?

发布于 2024-09-10 02:52:38 字数 326 浏览 6 评论 0原文

我们现在使用 .net 二进制序列化来序列化数据以进行持久化。它完成了这项工作,但并不像它打算用于此目的,因为更改数据并不是那么简单。

所以我开始研究 protobuf,因为它是围绕这个目的设计的。

我正在研究 protobuf-net 如何使用它。下面是一些我还不知道的事情。如果你已经得到答案,那将拯救我的日子。

  • 我不关心 .proto 文件,因为产品只是在 .net 中。那么我可以在没有 .proto 文件的情况下使用 protobuf-net 吗?
  • 如果是,如果需要移植到其他语言,我可以稍后从我的 C# 类中生成 .proto 文件吗

We use .net binary serialization right now to serialize data to persist. It does the job but doesn't fell like it's intended to use for that purpose as changing data is not so trivial.

So i started looking into protobuf as it is designed around that purpose.

i am looking at protobuf-net to how to use it. Below are some thing i don't know yet. if you got an answer already it will save my day.

  • I don't care about .proto files as product is just in .net. So can i use protobuf-net without having .proto files.
  • If yes, can i later generate .proto file out of my c# classes if need port to other language

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一直在等你来 2024-09-17 02:52:38

是的,protobuf-net 无需 .proto 文件即可工作。事实上,protobuf-net 的核心早在 .proto 支持之前就已经编写好了。您所需要的只是让它能够为您想要序列化的每个成员获取唯一的编号。在“v2”(未发布)中,您可以在外部执行此操作,但在可用的 dll 中,这意味着属性。这可以是定制的 protobuf-net 属性,或者您可以使用(例如)DataContractSerializer 属性(如果这样更方便):

[DataContract]
public class Foo {
    [DataMember(Order=1)]
    public int Abc {get;set;}

    [DataMember(Order=2)]
    public string Def {get;set;}
}

支持生成 .proto 文件 (Serializer.GetProto) - 但是,有一些注意事项:

  • protobuf-net 支持继承; Google protobuf 规范中没有定义继承。如果您认为互操作是一个问题,那么最好避免继承(尽管有一种解释它的方法;probofuf-net 总是发出有效的 protobuf 流)
  • 其他复杂类型可能需要解释;例如,虽然 floatdouble 直接映射到 protobuf 类型(在每个实现中都可用),但 decimal 不成立。代码>和<代码>日期时间;如果您认为互操作是一个问题,那么从一开始就值得简化它 - 例如,决定您要发送一个 DateTime,也许发送一个代表 long而是到纪元的偏移量?在任何实现中都易于解释。

Yes, protobuf-net works without .proto files. In fact, for protobuf-net the core was written long before the .proto support. All you need is something that allows it to obtain a unique number for each member you want to serialize. In "v2" (not released) you can do this externally, but in the available dll this means attributes. This can be the bespoke protobuf-net attributes, or you can use (for example) the DataContractSerializer attributes if that is more convenient:

[DataContract]
public class Foo {
    [DataMember(Order=1)]
    public int Abc {get;set;}

    [DataMember(Order=2)]
    public string Def {get;set;}
}

There is support for generating a .proto file (Serializer.GetProto) - however, there are some caveats:

  • protobuf-net supports inheritance; there is no definition of inheritance in the Google protobuf spec. If you think interop is a concern down the line, it may be best to avoid inheritance (although there is a way of interpreting it; probofuf-net always emits a valid protobuf stream)
  • other complex types may require interpretation; for example while float and double map directly to protobuf types (available in every implementation), that is not true of decimal and DateTime; if you think interop is an issue, it may be worth simplifying this from the start - for example deciding that you are going to send a DateTime, perhaps send a long that represents the offset into an epoch instead? easy to interpret in any implementation.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文