protobuf-net,它是怎么做的?
我们现在使用 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,protobuf-net 无需 .proto 文件即可工作。事实上,protobuf-net 的核心早在 .proto 支持之前就已经编写好了。您所需要的只是让它能够为您想要序列化的每个成员获取唯一的编号。在“v2”(未发布)中,您可以在外部执行此操作,但在可用的 dll 中,这意味着属性。这可以是定制的 protobuf-net 属性,或者您可以使用(例如)
DataContractSerializer
属性(如果这样更方便):是支持生成 .proto 文件 (
Serializer.GetProto
) - 但是,有一些注意事项:float
和double
直接映射到 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:There is support for generating a .proto file (
Serializer.GetProto
) - however, there are some caveats:float
anddouble
map directly to protobuf types (available in every implementation), that is not true ofdecimal
andDateTime
; 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 aDateTime
, perhaps send along
that represents the offset into an epoch instead? easy to interpret in any implementation.