Protobuf-net 将字符串字段反序列化为 C# guid
Protobuf-net deserialize string field to c# guid
在一个简化的场景中,假设我们有一个包含 guid 字段的自定义 c# 类型,对应的原型类型是
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;
}
当我尝试将此原型反序列化为 c# 类型时,我得到异常说明 'Invalid wire-type' 和 link 到 explanation 这对我没有帮助。是否有解决此问题的方法,还是我忽略了什么?
Protobuf-net 对 guid 有意见。那些在时间深处形成的观点,我现在后悔了,但在不伤人的情况下很难恢复。如果我今天写这篇文章是事后诸葛亮,是的:它可能只是序列化为一个字符串。但是:这不是它今天所期望的!
坦率地说,我会用阴影绕过它 属性。所以而不是
[ProtoMember(42)]
public Guid Foo {get;set;}
您可以使用:
public Guid Foo {get;set;}
[ProtoMember(42)]
private string FooSerialized {
get => Foo.ToString(); // your choice of formatting
set => Foo = Guid.Parse(value);
}
在 protobuf-net v3 中,这发生了变化;现在有一个名为 CompatibilityLevel
的概念适用于这种情况;如果 Guid
成员或包含类型的 CompatibilityLevel
为 300 或更多,则它被视为 string
。此处深入讨论了该主题:https://github.com/protobuf-net/protobuf-net/blob/main/docs/compatibilitylevel.md
在一个简化的场景中,假设我们有一个包含 guid 字段的自定义 c# 类型,对应的原型类型是
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;
}
当我尝试将此原型反序列化为 c# 类型时,我得到异常说明 'Invalid wire-type' 和 link 到 explanation 这对我没有帮助。是否有解决此问题的方法,还是我忽略了什么?
Protobuf-net 对 guid 有意见。那些在时间深处形成的观点,我现在后悔了,但在不伤人的情况下很难恢复。如果我今天写这篇文章是事后诸葛亮,是的:它可能只是序列化为一个字符串。但是:这不是它今天所期望的!
坦率地说,我会用阴影绕过它 属性。所以而不是
[ProtoMember(42)]
public Guid Foo {get;set;}
您可以使用:
public Guid Foo {get;set;}
[ProtoMember(42)]
private string FooSerialized {
get => Foo.ToString(); // your choice of formatting
set => Foo = Guid.Parse(value);
}
在 protobuf-net v3 中,这发生了变化;现在有一个名为 CompatibilityLevel
的概念适用于这种情况;如果 Guid
成员或包含类型的 CompatibilityLevel
为 300 或更多,则它被视为 string
。此处深入讨论了该主题:https://github.com/protobuf-net/protobuf-net/blob/main/docs/compatibilitylevel.md