使用 protobuf-net 反序列化消息列表

发布于 2024-09-19 03:41:26 字数 615 浏览 4 评论 0原文

我有一个使用 protobuf 发送消息的 java 后端,它通过 tib 在一个大字节数组 blob 中发送分隔的消息对象。我可以使用 java 中的函数 parseDelimitedFrom(yourStreamHere) 很好地反序列化它们,但在 C# 方面,我们遇到了一些问题,我找不到任何示例,但我可能在这里遗漏了一些明显的东西。

我们正在 C# 中做这样的事情

using (MemoryStream mem = new MemoryStream()) 
{ 
    mem.Write(byteArray, 0, byteArray.Length); 
    mem.Position = 0;     
    return Serializer.Deserialize<List<OrderState>>(mem); 
}

注意:我看到了一篇旧的 帖子 关于这一点,但它看起来相当过时,我认为从那时起 protobuf-net 发生了变化,但如果我错了,那就是正确的

I have a java backend that sends messages using protobuf, it sends delimited message objects in one big byte array blob over tib. I can deserialize them fine using the function parseDelimitedFrom(yourStreamHere) in java but on the C# side we are having some issues and I couldn't find any examples but I may just be missing something obvious here.

We are doing something in C# like this

using (MemoryStream mem = new MemoryStream()) 
{ 
    mem.Write(byteArray, 0, byteArray.Length); 
    mem.Position = 0;     
    return Serializer.Deserialize<List<OrderState>>(mem); 
}

Note: I saw an older post on this but it looked pretty dated and I think changes have occurred to protobuf-net since then but correct if I'm wrong there

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

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

发布评论

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

评论(1

冷月断魂刀 2024-09-26 03:41:47

开发人员昨天使用了标签 0 和前缀样式 128,就像这样

IEnumerable<SomeObject> list =  (Serializer.DeserializeItems<SomeObject>(memoryStream, PrefixStyle.Base128, 0));

,但我们仍然收到错误。今天,当我们在 C# 端调用 getProto 时,它似乎正在将设置为 double 类型的属性转换为fixed64 类型,在 java 端我们指定了 double,所以我认为这种不匹配导致了我们看到的错误。我们暂时将这些字段更改为字符串类型,现在上面的代码片段可以工作了。当然,理想情况下我们不想在不必要的时候发送字符串。

The developer was using tag 0 and prefix style 128 at one point yesterday like so

IEnumerable<SomeObject> list =  (Serializer.DeserializeItems<SomeObject>(memoryStream, PrefixStyle.Base128, 0));

but we were still getting an error. When we called getProto on the C# side today it appears it was converting our properties that were set to the double type to the fixed64 type, on the java side we had specified double so I think this mismatch was causing the errors we were seeing. We temporarily changed those fields to the string type and now the above snippet works. Of course ideally we don't want to send strings when we don't have to.

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