gpt4 book ai didi

c# - protobuf-net 支持命名元组吗?

转载 作者:行者123 更新时间:2023-12-02 20:43:46 27 4
gpt4 key购买 nike

protobuf-net是否支持named tuples的序列化?

例如

 [ProtoMember(1)]
protected readonly SortedDictionary<double, (double Bid, double Ask, double Open, double High, double Low, double Close, int Volume, int OpenInt)> FuturesCurveData;

最佳答案

“部分是,部分不是”。

protobuf-net 没有命名元组的特定知识,并且 the names are not available to library code ,但从 v2.2.0 开始,protobuf-net能够推断 ValueTuple<...> 的合约类型,本质上将其视为位置类型 - 所以 Bid将是字段 1, Ask将是字段 2 等。一些代码如 GetProto<T>不会产生您期望的结果(因为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<,> 工作。如果 key 类型是 intstring ,但它似乎遇到了一个错误,I have logged .

关于c# - protobuf-net 支持命名元组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45210999/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com