protobuf-net - 反引号、字典和.proto 文件

发布于 2024-08-26 12:19:55 字数 889 浏览 9 评论 0原文

我正在尝试使用 http 与使用 iPhone 上的 protobuf-net 的 C# 程序进行通信://code.google.com/p/metasyntropic/wiki/ProtocolBuffers

一行:

repeated Pair_Guid_List`1 Local = 6;

不幸的是,我得到的 .proto 文件(从 C# 源代码生成)包含 protoc 拒绝的 这是因为源数据是一个 C# 字典,以 Guid 键和类作为值。有没有办法更好地应对这个问题?

使用的 protobuf-net 版本是 r278.zip。

(C# 发送和接收这些 protobuf 都工作正常,只是当我们将 iphone 添加到混合中时,这才成为问题。)

更新:感谢 Marc,现在一切正常!

C# 端的对象是:

[ProtoMember(7)]
public Dictionary<Guid, List<Pages>> ReceivedPages { get; set; }

在 .proto: 中使用以下内容可以正常工作,

message PagesDict {
  required bcl.Guid guid = 1;
  repeated Pages Pages = 2;
}

相关消息包含:

  repeated PagesDict ReceivedPages = 7;

I'm trying to talk to a C# program that uses protobuf-net from an iphone using http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers

Unfortunately the .proto file I've been given (generated from the C# source code) includes an a line that protoc is rejecting:

repeated Pair_Guid_List`1 Local = 6;

It appears that this is because the source data is a C# Dictionary, with a Guid key and a class as the value. Is there a way to cope with this better?

The protobuf-net version in use is r278.zip.

(The C# sending and receiving these protobufs all works fine, it's just when we add the iphone into the mix that this becomes an issue.)

UPDATE: all working now thanks to Marc!

The object on the C# side turned out to be:

[ProtoMember(7)]
public Dictionary<Guid, List<Pages>> ReceivedPages { get; set; }

which worked fine using the following in the .proto:

message PagesDict {
  required bcl.Guid guid = 1;
  repeated Pages Pages = 2;
}

with the message in question containing:

  repeated PagesDict ReceivedPages = 7;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼疼热情 2024-09-02 12:19:55

首先 - 您是否尝试在 iPhone 上使用 protobuf-net ? v1 预计无法通过单点触控工作; v2 确实可以工作(这是 v2 工作的一大推动力),但尚未发布(它可用但目前不完整)。如果您想这样做,请告诉我,因为这很重要;-p

我希望他们通过调用 Serializer.GetProto 获得该 .proto,不幸的是,这并不是万无一失的,特别是当涉及像 Dictionary<,> 这样的事情时(我将添加一个 TODO 来尝试在 v2 中修复这个问题)。

好消息是,它将 Dictionary 建模为 repeated someType,其中 someType 应该是:

message someType {
    required keyType key = 1;
    required valueType value = 2;
}

Guid 被建模为 bcl.Guid (bcl.proto),即:

message Guid {
  optional fixed64 lo = 1; // the first 8 bytes of the guid
  optional fixed64 hi = 2; // the second 8 bytes of the guid
}

但是请注意,如果使用 .NET 工作,则根本不需要“proto” -。网;只是兼容类型。

Firstly - are you trying to use protobuf-net on the iPhone? v1 is not expected to work via monotouch; v2 does work (that was a big driver for the v2 work), but is not yet released (it is usable but incomplete at the moment). Let me know if you are trying to do this, as it would matter ;-p

I expect they've obtained that .proto by calling Serializer.GetProto<T>, which is unfortunately not fool-proof, especially when things like Dictionary<,> are involved (I'll add a TODO to try to fix that in v2).

The good news is that it models Dictionary<TKey,TValue> as repeated someType, where someType should be:

message someType {
    required keyType key = 1;
    required valueType value = 2;
}

And Guid is modelled as bcl.Guid (bcl.proto), which is:

message Guid {
  optional fixed64 lo = 1; // the first 8 bytes of the guid
  optional fixed64 hi = 2; // the second 8 bytes of the guid
}

Note, however, that no "proto" is needed at all if working .NET-to-.NET; just compatible types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文