在协议缓冲区中表示System.Decimal的最佳方法是什么?

继这个问题之后,在协议缓冲区中表示System.Decimal对象的最佳方法是什么?

那么,protobuf-net将为您处理这个问题; 它运行类型的属性,并完全支持decimal 。 由于在proto中没有直接表达decimal方法,它不会(当前)从“.proto”文件生成decimal属性,但是识别一些常见类型(“BCL.Decimal”或类似的)并将其解释为十进制。

至于代表它 – 我在protobuf-net wiki区域有一个关于这个(现在我怀疑已过时)的讨论文件 ; 现在有一个protobuf-net的工作版本,它只是为你做的。

毫无疑问,乔恩和我将在今天晚些时候更多地解决这个问题;-p

这个(在.proto中)的protobuf-net版本就像(从这里 ):

 message Decimal { optional uint64 lo = 1; // the first 64 bits of the underlying value optional uint32 hi = 2; // the last 32 bis of the underlying value optional sint32 signScale = 3; // the number of decimal digits, and the sign } 

Marc和我有一个非常模糊的计划,想出一个“常见的PB消息”库,这样你就可以用一种常见的方式表示非常常见的类型(日期/时间和十进制弹跳),在.NET和Java中可用转换(以及任何其他人想要贡献的东西)。

如果你很乐意坚持使用.NET,而且你正在寻找紧凑性,我可能会选择以下内容:

 message Decimal { // 96-bit mantissa broken into two chunks optional uint64 mantissa_msb = 1; optional uint32 mantissa_lsb = 2; required sint32 exponent_and_sign = 3; } 

符号可以用exponent_and_sign的符号表示,指数是绝对值。

使尾数的两个部分都可选意味着0表示非常紧凑(但仍然区分0m和0.0000m等)。 如果我们真的想要,exponent_and_sign也可以是可选的。

我不知道Marc的项目,但在我的端口中我生成了部分类,因此您可以将System.Decimal和Protobuf.Common.Decimal(或其他)之间的转换放入分部类中。

我为protobuf-csharp-port添加了一个带有钩子的补丁,它使用原生的Decimal和DateTime结构生成protobuf类。 有线格式,它们由两个“内置”原型消息表示。

以下是链接: https : //code.google.com/p/protobuf-csharp-port/issues/detail?can = 2&start = 0&num = 100&q =&cfpec = ID%20Type%20Status%20Priority%20Milestone%20Owner%20Summary&groupby =&排序=&ID = 78