使用 protobuf-net 反序列化消息列表
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
开发人员昨天使用了标签 0 和前缀样式 128,就像这样
,但我们仍然收到错误。今天,当我们在 C# 端调用 getProto 时,它似乎正在将设置为 double 类型的属性转换为fixed64 类型,在 java 端我们指定了 double,所以我认为这种不匹配导致了我们看到的错误。我们暂时将这些字段更改为字符串类型,现在上面的代码片段可以工作了。当然,理想情况下我们不想在不必要的时候发送字符串。
The developer was using tag 0 and prefix style 128 at one point yesterday like so
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.