使用 Protobuf-net 序列化对象列表
我一直在寻找对文件进行一些二进制序列化,而 protobuf-net 似乎是一个性能良好的替代方案。不过,我在开始时有点卡住了。因为我想将类的定义与实际序列化分离,所以我不使用属性而是选择使用 .proto 文件,我已经得到了对象的结构(我认为)
message Post {
required uint64 id = 1;
required int32 userid = 2;
required string status= 3;
required datetime created = 4;
optional string source= 5;
}
(日期时间有效还是应该使用刻度作为 int64?)
但我坚持如何使用 protogen,然后将 Post 的 IEnumerable 序列化到文件并将其读回。任何帮助将不胜感激
另一个相关问题,是否有任何最佳实践来检测损坏的二进制文件,例如计算机在序列化时关闭
I've been looking to do some binary serialization to file and protobuf-net seems like a well-performing alternative. I'm a bit stuck in getting started though. Since I want to decouple the definition of the classes from the actual serialization I'm not using attributes but opting to go with .proto files, I've got the structure for the object down (I think)
message Post {
required uint64 id = 1;
required int32 userid = 2;
required string status= 3;
required datetime created = 4;
optional string source= 5;
}
(is datetime valid or should I use ticks as int64?)
but I'm stuck on how to use protogen and then serialize a IEnumerable of Post to a file and read it back. Any help would be appreciated
Another related question, is there any best practices for detecting corrupted binary files, like if the computer is shut down while serializing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Re
DateTime
...这不是标准原型类型;我已经在我自己的库中添加了 BCL.DateTime (或类似的),其目的是匹配 protobuf-net 用于DateTime
的内部序列化,但我相当确定我还没有(还)更新了代码生成器以将其检测为特殊情况。如果您想让我尝试,添加起来相当容易...如果您想要最大的可移植性,“蜱”式的方法可能是务实的。让我知道...重新序列化到文件 - if 应该与 入门示例,但请注意 protobuf-net 希望使用它可以重建的数据; 只是
IEnumerable
可能会导致问题 -IList
应该没问题(它默认为List
T>
作为重建时的具体类型)。重新损坏 - 可能使用
SerializeWithLengthPrefix
- 它甚至可以在消息边界检测到问题(否则它们作为 EOF 无法检测到)。这(顾名思义)首先写入长度,因此它知道是否有足够的数据(通过DeserializeWithLengthPrefix
)。或者,保留文件中的前 [n] 个字节作为哈希/校验和。写入此空白间隔符,然后写入数据,计算哈希/校验和并覆盖开头。在反序列化期间进行验证。还有很多工作。Re
DateTime
... this isn't a standard proto type; I have added a BCL.DateTime (or similar) to my own library, which is intended to match the internal serialization that protobuf-net uses forDateTime
, but I'm fairly certain I haven't (yet) updated the code-generator to detect this as a special-case. It would be fairly easy to add if you want me to try... If you want maximum portability, a "ticks" style approach might be pragmatic. Let me know...Re serializing to a file - if should be about the same as the Getting Started example, but note that protobuf-net wants to work with data it can reconstruct; just
IEnumerable<T>
might cause problems -IList<T>
should be fine, though (it'll default toList<T>
as a concrete type when reconstructing).Re corruption - perhaps use
SerializeWithLengthPrefix
- it can then detect issues even at a message boundary (where they are otherwise undetectable as an EOF). This (as the name suggests) writes the length first, so it knows whether is has enough data (viaDeserializeWithLengthPrefix
). Alternatively, reserve the first [n] bytes in your file for a hash / checksum. Write this blank spacer, then the data, calculate the hash / checksum and overwrite the start. Verify during deserialization. Much more work.