3

I am learning google protocol buffer since yesterday and have a question :

Can i use a user-defined java class as a field type in a .proto file?

Let me clarify my questions with the details below:

1 - I have following java class "MyComplexClass.java":

package mypackage;

import another.package1.ClassA;
import another.package2.ClassB;

public class MyComplexClass {

private ClassA var1;
private ClassB var2;

public MyComplexClass(ClassA X, ClassB Y)
 this.var1 = X;
 this.var2 = Y;
}

2- Now I would like to serialize an instance of the class "MyComplexClass.java". For that purpose, I would like to describe a message like the following in a .proto file:

message myMessageToBeSerialized {

 required ***MyComplexClass*** intanceOfComplexClass = 1;

}

Is it possible to use the user-defined class MyComplexClass as a field type ? Or is it only allowed to use scalar types?

Any help would be appreciated. Thanks in advance.

Horace

Horace
  • 1,198
  • 2
  • 17
  • 31

3 Answers3

3

No. It's only possible to use types defined as protocol buffers. (That said, it's common to implement your own complex classes as wrappers around protocol buffer classes, when you need to add more functionality.)

If you were really, really keen on it, you could use normal Java serialization (an ObjectOutputStream wrapped around a ByteArrayOutputStream), and then send the byte[] through the protocol buffers, though.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Thank you Louis. But it is said that google protocol buffer is much faster than the java native serialization and that is the advantage I want to use it. But nevertheless I would find it comfortable to just give a complex user-defined to serialization just as I can do it with the java native serialization. – Horace Apr 19 '12 at 14:33
  • The options I've listed are probably the only options, I'm afraid. You cannot give the protobuffers a class and tell it to serialize it; protobuffers can only serialize the classes generated by protoc. – Louis Wasserman Apr 19 '12 at 14:36
  • You have to use protocol buffers to send the messages, yes. The main way to do that in the context of a Java application is to make the classes you're shipping back and forth simple wrappers around protocol buffer message classes. What information is in your classes that doesn't fit into the protocol buffer scheme? What's the problem? – Louis Wasserman Apr 19 '12 at 14:56
  • I don't know enough to help you, but the approaches I'm discussing above are the only alternatives available with protocol buffers. That said, it's also a legit approach to pass around some things with protocol buffers and some things without protocol buffers, although you probably wouldn't be able to take advantage of the protobuf RPC abstraction. Honestly, I'm thinking that the Java-serialization-to-byte-array approach might be the simplest place to start, and try optimizing it more later. – Louis Wasserman Apr 19 '12 at 15:44
1

Yes you can. Define both ClassA and ClassB as messages and you can refer to them as field types in MyComplexClass. See below. (Define all 3 packages in seperate .proto files)

package another.package1;
message ClassA 
{
  <fields>
}


package another.package2;
message ClassB
{
  <fields>  
}


package mypackage;

import another/package1/ClassA;
import another/package2/ClassB;

message MyComplexClass
{
     required ClassA var1 = 1;
     required ClassB var2 = 2;  
}
user572964
  • 600
  • 5
  • 12
0

Simply define a proxy class that implements the conversion from the protocol buffer class to your class and vice versa.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176