我可以将 c# class 反序列化为 Python class 吗?
Can I DeserializeWithLengthPrefix a c# class into a Python class?
我有很多数据在 c# 中使用 ProtoBuf 序列化。
一位同事想用 Python 阅读这些文件。
这可以实现吗,因为 Python class 与 c# 不同。
这是 c# class:
[ProtoContract]
public class TickRecord
{
[ProtoMember(1, DataFormat = DataFormat.FixedSize, IsRequired = true)]
public DateTime DT;
[ProtoMember(2, DataFormat = DataFormat.FixedSize, IsRequired = true)]
public double BidPrice;
[ProtoMember(3, DataFormat = DataFormat.FixedSize, IsRequired = true)]
public double AskPrice;
[ProtoMember(4, DataFormat = DataFormat.FixedSize, IsRequired = true)]
public int BidSize;
[ProtoMember(5, DataFormat = DataFormat.FixedSize, IsRequired = true)]
public int AskSize;
}
看起来像 protobuf-net
;幸运的是,该库包含一个工具,可以根据它对您的模型的看法吐出 .proto 模式:
var schema = Serializer.GetProto<TickRecord>();
结果为:
message TickRecord {
required bcl.DateTime DT = 1;
required double BidPrice = 2 [default = 0];
required double AskPrice = 3 [default = 0];
required sfixed32 BidSize = 4 [default = 0];
required sfixed32 AskSize = 5 [default = 0];
}
其中 bcl.DateTime
是 "bcl.proto" 中描述的 protobuf-net 复杂数据类型。
坦率地说,我建议使用 unix 时间将您的日期时间更改为 long
- 它会使这里的事情变得更简单。那给你:
message TickRecord {
required sfixed64 DT = 1 [default = 0];
required double BidPrice = 2 [default = 0];
required double AskPrice = 3 [default = 0];
required sfixed32 BidSize = 4 [default = 0];
required sfixed32 AskSize = 5 [default = 0];
}
您可以将其提供给您的同事,他们可以 运行 protoc
或他们需要的任何其他 platform/library 环境中用于协议缓冲区的工具,并且应该对其进行设置。序列化格式是有意跨平台的,因此 data 应该可以正常工作。
注意:*WithLengthPrefix
通过将数据包装在外部 shell 中来工作。我需要查看您使用的确切参数,以建议如何建议您的同事。
注意:将 DateTime
更改为 long
是一项重大更改;如果您做不到,您的同事将不得不查看 bcl.proto(只是 Google)并将其翻译成 python.
我有很多数据在 c# 中使用 ProtoBuf 序列化。
一位同事想用 Python 阅读这些文件。
这可以实现吗,因为 Python class 与 c# 不同。
这是 c# class:
[ProtoContract]
public class TickRecord
{
[ProtoMember(1, DataFormat = DataFormat.FixedSize, IsRequired = true)]
public DateTime DT;
[ProtoMember(2, DataFormat = DataFormat.FixedSize, IsRequired = true)]
public double BidPrice;
[ProtoMember(3, DataFormat = DataFormat.FixedSize, IsRequired = true)]
public double AskPrice;
[ProtoMember(4, DataFormat = DataFormat.FixedSize, IsRequired = true)]
public int BidSize;
[ProtoMember(5, DataFormat = DataFormat.FixedSize, IsRequired = true)]
public int AskSize;
}
看起来像 protobuf-net
;幸运的是,该库包含一个工具,可以根据它对您的模型的看法吐出 .proto 模式:
var schema = Serializer.GetProto<TickRecord>();
结果为:
message TickRecord {
required bcl.DateTime DT = 1;
required double BidPrice = 2 [default = 0];
required double AskPrice = 3 [default = 0];
required sfixed32 BidSize = 4 [default = 0];
required sfixed32 AskSize = 5 [default = 0];
}
其中 bcl.DateTime
是 "bcl.proto" 中描述的 protobuf-net 复杂数据类型。
坦率地说,我建议使用 unix 时间将您的日期时间更改为 long
- 它会使这里的事情变得更简单。那给你:
message TickRecord {
required sfixed64 DT = 1 [default = 0];
required double BidPrice = 2 [default = 0];
required double AskPrice = 3 [default = 0];
required sfixed32 BidSize = 4 [default = 0];
required sfixed32 AskSize = 5 [default = 0];
}
您可以将其提供给您的同事,他们可以 运行 protoc
或他们需要的任何其他 platform/library 环境中用于协议缓冲区的工具,并且应该对其进行设置。序列化格式是有意跨平台的,因此 data 应该可以正常工作。
注意:*WithLengthPrefix
通过将数据包装在外部 shell 中来工作。我需要查看您使用的确切参数,以建议如何建议您的同事。
注意:将 DateTime
更改为 long
是一项重大更改;如果您做不到,您的同事将不得不查看 bcl.proto(只是 Google)并将其翻译成 python.