Google protocol buffers - custom java objects as message fields

I have been studying the google protocol buffer since yesterday and asking a question:

Can I use a custom java class as a field type in a .proto file?

Let me clarify my questions below:

1 - I have the 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 this purpose, I would like to describe the following message in a .proto file:

message myMessageToBeSerialized {

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

}

Is it possible to use a custom class MyComplexClass as a field type? Or is it allowed to use only scalar types?

Any help would be greatly appreciated. Thanks in advance.

Horace

+3
source share
3 answers

. , . ( , , .)

, Java (ObjectOutputStream, ByteArrayOutputStream), [] .

+2

, . ClassA, ClassB , MyComplexClass. . ( 3 .proto)

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;  
}
+1

-, .

0

All Articles