Reputation: 31
This is the way my classes are modelled. sorry it's long:
[Serializable]
[DataContract]
public class RequestSection
{
[DataMember(Order = 1)]
public List<BaseA> Allrequests;
public RequestSection()
{
Allrequests = new List<BaseA>();
}
}
[Serializable]
[DataContract]
[ProtoInclude(2, typeof(BaseA<derResponse1>))]
[ProtoInclude(3, typeof(BaseA<derResponse2>))]
public abstract class BaseA
{
[DataMember(Order = 1)]
public int baseA = 10;
}
[Serializable]
[DataContract]
[ProtoInclude(2, typeof(der1))]
[ProtoInclude(3, typeof(der2))]
public abstract class BaseA<T> : BaseA where T : ResponseBaseA, new()
{
T _Response;
/// <summary>
///
/// </summary>
[System.Runtime.Serialization.DataMember(Order = 1)]
public new T Response
{
get { return _Response; }
set { _Response = value; }
}
}
[Serializable]
[DataContract]
public class der1 : BaseA<derResponse1>
{
[DataMember(Order = 1)]
public int derive1 = 20;
}
[Serializable]
[DataContract]
public class der2 : BaseA<derResponse2>
{
[DataMember(Order = 1)]
public int derive2 = 30;
}
[Serializable]
[DataContract]
[ProtoInclude(2, typeof(derResponse1))]
[ProtoInclude(3, typeof(derResponse2))]
public abstract class ResponseBaseA
{
[DataMember(Order = 1)]
public int responseBaseA = 100;
}
[Serializable]
[DataContract]
public class derResponse1 : ResponseBaseA
{
[DataMember(Order = 1)]
public int derResp1 = 200;
}
[Serializable]
[DataContract]
public class derResponse2 : ResponseBaseA
{
[DataMember(Order = 1)]
public int derResp2 = 300;
}
}
the way we create them is here
RequestSection section = new RequestSection();
der1 der1 = new der1();
der2 der2 = new der2();
section.Allrequests.Add(der1);
section.Allrequests.Add(der2);
Iam not able to serialize section using protobuf-net(both v1 and v2 - they say unknown subtype) so im trying runtime model.
Here is the proto file iam using.
message RequestSection{
repeated BaseA requests=1;
}
message BaseA{
optional int32 baseA=1;
optional BaseA1Generic BaseA1Generic =2;
optional BaseA2Generic BaseA2Generic =3;
}
message BaseA1Generic{
optional ResponseBaseA baseResponse =1;
optional Der1 requestDer1 = 2;
}
message BaseA2Generic{
optional ResponseBaseA baseResponse =1;
optional Der2 requestDer2 = 3;
}
message ResponseBaseA{
optional int32 responseBaseA = 1;
optional derResponse1 derivedResponse1 =2;
optional derResponse2 derivedResponse2 =3;
}
message derResponse1{
optional int32 derResponse1 = 1;
}
message derResponse2{
optional int32 derResponse2 = 1;
}
message Der1{
optional int32 d1=1;
}
message Der2{
optional int32 d2=1;
}
Its not deserializing on Java side and getting only this
requests {
baseA: 10
1000: "\302>\002\b\024"
}
requests {
baseA: 10
1001: "\302>\002\b\036"
}
I am trying to make my proto file generic because I may have any class(der1/der2/ or more in future) under section requests.
Upvotes: 1
Views: 1413
Reputation: 1062600
The problem at the moment is that your model specifies both of der1
and der2
for both of BaseA<derResponse1>
and BaseA<derResponse2>
, meaning there are two routes to der1
/der2
. In v2, we can avoid this by specifying the inheritance explicitly per closed generic type; so: remove the two [ProtoInclude(...)]
from BaseA<T>
, and instead, use:
RuntimeTypeModel.Default[typeof(BaseA<derResponse1>)].AddSubType(2,typeof(der1));
RuntimeTypeModel.Default[typeof(BaseA<derResponse2>)].AddSubType(3,typeof(der2));
As a side note; it is not required that they use different numbers here, since they are independent. They could both use field 2
is you like; but using different field-numbers is fine too.
Upvotes: 2