[英]Protobuf-net deserialize string field to c# guid
In a simplified scenario, assume we have a custom c# type which contains a guid field and the corresponding proto type is,在一个简化的场景中,假设我们有一个自定义的 c# 类型,它包含一个 guid 字段,相应的原型是,
message HelloReply {
string message = 1;
string guid_id = 2; // this is defined GUID in corresponding c# type
repeated string names = 3;
map<string, double> prices = 4;
}
When I try to deserialize this proto to c# type, I get exception stating 'Invalid wire-type' and a link to explanation which is not helpful to me.当我尝试将这个 proto 反序列化为 c# 类型时,我收到异常声明“无效的线类型”和一个对我没有帮助的解释链接。 Is there a work around for this or am I overlooking something ?
有解决这个问题的方法还是我忽略了什么?
In protobuf-net v3, this changes;在 protobuf-net v3 中,这发生了变化; there is now a concept called
CompatibilityLevel
, that works for this scenario;现在有一个名为
CompatibilityLevel
的概念,适用于这种情况; if a Guid
member or containing type has CompatibilityLevel
of 300 or more, then it is treated as a string
.如果
Guid
成员或包含类型的CompatibilityLevel
为 300 或更高,则将其视为string
。 This topic is discussed in depth here: https://github.com/protobuf-net/protobuf-net/blob/main/docs/compatibilitylevel.md这个话题在这里有深入讨论: https : //github.com/protobuf-net/protobuf-net/blob/main/docs/compatibilitylevel.md
Protobuf-net has opinions on guids. Protobuf-net 对 guid 有意见。 Opinions that were forged back in the depth of time, and that I now regret, but which are hard to revert without breaking people.
在时间的深处伪造的意见,现在我后悔了,但在不破坏人们的情况下很难恢复。 If I was writing this today with hindsight, yes: it would probably just serialize as a string.
如果我今天事后写这个,是的:它可能只是序列化为一个字符串。 But: that isn't what it expects today!
但是:这不是它今天所期望的!
Frankly I'd hack around it with a shadow property.坦率地说,我会用影子属性来解决它。 So instead of
所以代替
[ProtoMember(42)]
public Guid Foo {get;set;}
You could use:你可以使用:
public Guid Foo {get;set;}
[ProtoMember(42)]
private string FooSerialized {
get => Foo.ToString(); // your choice of formatting
set => Foo = Guid.Parse(value);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.