Your approach actually won't work (at least, as you've written the code)
because if you write two messages to a stream without any sort of delimiter,
it's impossible to figure out later where one message ends and the other
starts.
But I suggest doing this instead:
message A {
...
}
message B {
required A a = 1;
...
}
In other words, change inheritance to composition.
On Mon, Aug 3, 2009 at 2:00 PM, Tai wrote:
>
> Hi again,
>
> I have two classes where B extends from A. Since both classes are
> legacy and as far as I understood protocol buffers I write a proto
> file for class A. For serialization and deserialization I just add the
> methods writeObject() and readObject() to class A.
>
> Now I wonder how to write the proto file for class B which is a
> subclass of A? Is it like this that I define in the B proto file the
> attributes defined in B and then in Class B's methods I do this:
>
>private void writeObject(java.io.ObjectOutputStream out) throws
> IOException {
>Builder builder = A.AttributeA.newBuilder();
>builder.setAttributeA(attributeA);
>A.AttributeA message = builder.build();
>message.writeTo(out);
>
>Builder builder2 = B.AttributeB.newBuilder();
>builder2.setAttributeB(attributeB);
>B.AttributeB message2 = builder2.build();
>message2.writeTo(out);
>}
>
>private void readObject(java.io.ObjectInputStream in)
>throws ClassNotFoundException, IOException {
>A.AttributeA message = A.AttributeA.parseFrom(in);
>attributeA = message.getAttributeA();
>
>B.AttributeB message2 = B.AttributeB.parseFrom(in);
>attributeB = message2.getAttributeB();
>}
>
> Is this the standard approach?
>
> Thanks Tai
> >
>
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com
To unsubscribe from this group, send email to
protobuf+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/protobuf?hl=en
-~--~~~~--~~--~--~---