Protobuf,如何在二进制文件中添加自定义版本详细信息
我想用我自己的版本号标记每个 protobuf 序列化数据。这样我就可以根据需要检查使用哪个版本来序列化数据。虽然protobuf是围绕理念设计的,所以你不需要检查版本。
为此,我可以使用我们在 .net 序列化中所做的相同方式,在其中添加版本字段。
只是想确认这也是 protobuf 中的首选方法。
class protodata1
{
public protodata1()
{
version = 1;
}
[Protomember(1)]
int version { get; set;}
[Protomember(2)]
int somedata { get; set;}
}
还应该像上面那样在 protobuf-net 中完成吗? 在构造函数中分配版本,如果类被期望化,该版本将被覆盖。对于要序列化的新对象,它将使用版本 1
I want to tag each protobuf serialized data with my own version number. so i can check which version was used to serialize data if required. Although protobuf is designed around idea so you don't need to check version.
To do that i can use same way we have been doing in .net serialization add version field in it.
Just want to confirm that this is the preferred way of doing in protobuf as well.
class protodata1
{
public protodata1()
{
version = 1;
}
[Protomember(1)]
int version { get; set;}
[Protomember(2)]
int somedata { get; set;}
}
Also should be done like above in protobuf-net?
assign version in constructor which will get override if class is getting desiralized. for new objects to be serialized it will use version 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,如果“版本”的含义只是作为一个数据字段,那么当然;只需将其添加为序列化成员即可。请注意,这在序列化期间不会有任何特殊含义(例如,通过替代合约运行它) - 尽管顺便说一句,如果您需要支持可能在“v2”中存在选项<完全不同的消息结构(无论如何都是个坏主意)。
我可能要补充的唯一警告是,任何未已包含此数字的现有数据都将声称是“版本 1”。在“v2”中,另一个选择是使用跳过构造函数的 WCF 方法(如果需要)。这意味着这些情况默认为“版本 0”——也许不那么令人困惑(或者可能更令人困惑;我会让你决定)。
Well, if your meaning of "version" is simply as a data-field, then sure; just add it as a serialized member. Note that this won't have any special meaning during serialization (running it through an alternative contract, for example) - although as an aside there might be options here in "v2" if you need to support radically different message structures (a bad idea anyway).
The only caveat I might add is that any existing data that didn't already include this number will claim to be "version 1". In "v2" another option is to use the WCF approach of skipping the constructor (if you require). This will mean that these cases default instead to "version 0" - perhaps less confusing (or perhaps more confusing; I'll let you decide).