Java Protobuf(版本2.4.1)和Protobuf net(版本r480)继承兼容性

Java Protobuf(版本2.4.1)和Protobuf net(版本r480)继承兼容性,java,inheritance,compatibility,protocol-buffers,protobuf-net,Java,Inheritance,Compatibility,Protocol Buffers,Protobuf Net,使用Java Protobuf是否可以反序列化以下使用Protobuf net序列化的DerivedMessage namespace Test.Protobuf { [ProtoBuf.ProtoContract] [ProtoBuf.ProtoInclude(2550, "Test.Protobuf.DerivedMessage")] class BaseMessage { [ProtoBuf.ProtoMember(1)] public string Mes

使用Java Protobuf是否可以反序列化以下使用Protobuf net序列化的DerivedMessage

namespace Test.Protobuf
{
  [ProtoBuf.ProtoContract]
  [ProtoBuf.ProtoInclude(2550, "Test.Protobuf.DerivedMessage")]
  class BaseMessage
  {
    [ProtoBuf.ProtoMember(1)]
    public string MessageId { get; set; }

    [ProtoBuf.ProtoMember(2)]
    public int CommandId { get; set; }
  }

  [ProtoBuf.ProtoContract]
  class DerivedMessage : BaseMessage
  {
    [ProtoBuf.ProtoMember(1)]
    public int ClientId { get; set; }

    [ProtoBuf.ProtoMember(2)]
    public string Message { get; set; }

    [ProtoBuf.ProtoMember(3)]
    public string Description { get; set; }
  }
}
DerivedMessage扩展了BaseMessage,读取文档解决方案似乎是在原始文件中使用嵌套消息 但它不起作用,这里是我的原始文件:

package protobuf;

option java_package = "test.protobuf";
option java_outer_classname = "Proto";

message BaseMessage {
    optional string message_id = 1;
    optional int32 command_id = 2;
}

message DerivedMessage {
    optional int32 client_id = 1;
    optional string token = 2;
    optional string message = 3;
    optional string description = 4;
    optional BaseMessage base_message = 5;
}
有解决方案吗


谢谢

字段号必须匹配,并进入BaseMessage,而不是DerivedMessage

在不相关的新闻中,您在DerivedMessage中的其他字段看起来有问题;“token”来自number,后面的2个字段被1关闭

message BaseMessage {
    optional string message_id = 1;
    optional int32 command_id = 2;
    optional DerivedMessage derived_message = 2550;
}

message DerivedMessage {
    optional int32 client_id = 1;
    optional string message = 2;
    optional string description = 3;
}

派生类是否也不应该具有与父类相同ID的字段?在您的示例中,messageId和clientId都具有相同的标识符:1。

不,这根本不是问题。Hi Mark,按照您的说明,我现在可以反序列化Protobuf net序列化的消息。现在的问题是Protobuf net无法对Java Protobuf序列化的消息进行反序列化,它会抛出一个“无法将'Test.Protobuf.BaseMessage'类型的对象强制转换为'Test.Protobuf.DerivedMessage'类型”异常。问题似乎在于写入字节的顺序。事实上,设置ClientId=3,CommandId=5 Protobuf net系列化b2 9f 01 02 08 03 10 05,而Java Protobuf系列化10 05 b2 9f 01 02 08 03不幸的是,我无法更改.net端类。@user1406564 v2,对吗?令人恼火的是,v1解决了这个问题(尽管效率稍低)。不可否认,这是protobuf网络的一个失败;它应该(并且确实)接受无序的伪继承。我可以试着解决这个问题,但不会马上解决。不幸的是,protobuf根本不支持继承,对protobuf网络实现的支持必须有点厚颜无耻。我责备我。