I'm doing communications between a .Net-based program on a Windows system and Android devices. On the .Net end I'm using Marc Gravell's great protobuf-net program, and on the Android end I'm using David Yu's great protostuff program.
My procedure (so far) is to use the .Net classes as the defining classes. I use the protobuf-net Serializer.GetProto() method to generate a .proto file, and protostuff's protostuff-compiler program to generate Java classes that correspond to the .Net classes, more-or-less.
This seems to work fairly well, except that I've run into a problem with inheritance. Yes, I know, inheritance isn't supposed to work with protocol buffers. But both protobuf-net and protostuff have implemented support for inherited classes, each in its own way.
So my question is, does anyone have any suggestion of a simple way to get inherited C# classes to map to inherited Java classes, and vice-versa?
Here's an example of what I'm working with. These are the C# classes:
public /* abstract */ class ProgramInfoBase
{
private string _programName;
private string _programVersion;
[ProtoMember(1)]
public string ProgramName
{
get { return _programName; }
set { _programName = value; }
}
[ProtoMember(2)]
public string ProgramVersion
{
get { return _programVersion; }
set { _programVersion = value; }
}
}
public class ProgramInfoAndroid : ProgramInfoBase
{
private string _androidDeviceName;
[ProtoMember(1)]
public string AndroidDeviceName
{
get { return _androidDeviceName; }
set { _androidDeviceName = value; }
}
}
public class ProgramInfoWindows : ProgramInfoBase
{
private string _windowsMachineName;
[ProtoMember(1)]
public string WindowsMachineName
{
get { return _windowsMachineName; }
set { _windowsMachineName = value; }
}
}
And here's one of my .proto files:
package Merlinia.MessagingDefinitions;
option java_package = "com.Merlinia.MMessaging_Test.protostuff";
message ProgramInfoAndroid {
optional string AndroidDeviceName = 1;
}
message ProgramInfoBase {
optional string ProgramName = 1;
optional string ProgramVersion = 2;
// the following represent sub-types; at most 1 should have a value
optional ProgramInfoAndroid ProgramInfoAndroid = 1001;
optional ProgramInfoWindows ProgramInfoWindows = 1002;
}
message ProgramInfoWindows {
optional string WindowsMachineName = 1;
}
Running that through protostuff's protostuff-compiler program produces three separate Java classes, which is to be expected. But what I'd like is to have it generate the corresponding C# class inheritance for the Java classes, and for the serialization and deserialization between protobuf-net and protostuff to support the inherited classes at both ends.
EDIT:
I've now changed my mind. Please see the following question: How to get protobuf-net to flatten and unflatten inherited classes in .Net?