protobuf-net是否支持named tuples的序列化?
例如
SELECT
COUNT(main_dish),
COUNT(soup_stew),
COUNT(meat_fish),
COUNT(other),
COUNT(main_dish) + COUNT(soup_stew) + COUNT(meat_fish) + COUNT(other) AS 'total' FROM foodlist;
答案 0 :(得分:3)
“部分是,部分不是”。
protobuf-net没有命名元组的特定知识和the names are not available to library code,但从v2.2.0起,protobuf-net 能推断出ValueTuple<...>
类型的合同,主要将其视为位置类型 - 因此Bid
将为字段1,Ask
将为字段2,等等。GetProto<T>
等代码将< em> not 产生你期望的结果(因为a:它看不到名字,而b:大型元组的形状变得很奇怪),你将无法控制细粒度的序列化细节(DataFormat
等),但是:它应该有效。
以下工作正常:
using ProtoBuf;
using System.Collections.Generic;
static class P
{
static void Main()
{
var obj = new MyType { FuturesCurveData = {
{ 1.0, (1, 2, 3, 4, 5, 6, 7, 8) },
{ 2.0, (2, 3, 4, 5, 6, 7, 8, 9) },
} };
var clone = Serializer.DeepClone(obj);
foreach(var pair in clone.FuturesCurveData)
{
System.Console.WriteLine($"{pair.Key}: {pair.Value}");
}
}
}
[ProtoContract]
class MyType
{
[ProtoMember(1)]
public SortedDictionary<double, (double Bid, double Ask, double Open, double High, double Low, double Close, int Volume, int OpenInt)> FuturesCurveData { get; } =
new SortedDictionary<double, (double Bid, double Ask, double Open, double High, double Low, double Close, int Volume, int OpenInt)>();
}
输出:
1: (1, 2, 3, 4, 5, 6, 7, 8)
2: (2, 3, 4, 5, 6, 7, 8, 9)
这就是GetProto<MyType>()
所产生的 - 并不是那么成功:
syntax = "proto2";
package System;
message KeyValuePair_Double_ValueTuple_Double_Double_Double_Double_Double_Double_Int32_ValueTuple_Int32 {
optional double Key = 1;
optional ValueTuple_Double_Double_Double_Double_Double_Double_Int32_ValueTuple_Int32 Value = 2;
}
message MyType {
repeated KeyValuePair_Double_ValueTuple_Double_Double_Double_Double_Double_Double_Int32_ValueTuple_Int32 FuturesCurveData = 1;
}
message ValueTuple_Double_Double_Double_Double_Double_Double_Int32_ValueTuple_Int32 {
optional double Item1 = 1;
optional double Item2 = 2;
optional double Item3 = 3;
optional double Item4 = 4;
optional double Item5 = 5;
optional double Item6 = 6;
optional int32 Item7 = 7;
optional ValueTuple_Int32 Rest = 8;
}
message ValueTuple_Int32 {
optional int32 Item1 = 1;
}
作为旁注,如果密钥类型为map<,>
或int
,则应作为string
工作,但它似乎遇到了错误,I have logged。