I'm using a protobuf java file inside a class which gets serialized (using 
the default Java serialization) and I noticed that the extensions aren't 
available after de-serializing the object.

*This is my proto file:*

message ExtensionTest {
> extensions 100 to max;
>
> optional string parentAttribute = 1;
>
> message ExtensionItem{
>     extend ExtensionTest{
>         optional ExtensionItem item = 100; // Unique extension number
>     }
>     optional string childAttribute = 1;
> }
> }


*So first i create a new ExtensionTest and set an extension:*

ExtensionTest.Builder testBuilder = ExtensionTest.newBuilder();
> testBuilder.setParentAttribute("tst");
> ExtensionItem.Builder itemBuilder = ExtensionItem.newBuilder();
> itemBuilder.setChildAttribute("childtst");
> testBuilder.setExtension(ExtensionTest.ExtensionItem.item, 
> itemBuilder.build());
>
> ExtensionTest oldObj = testBuilder.build();
> System.out.println("oldObj: " + oldObj); 


*This results in the following output:*

oldObj: parentAttribute: "tst"
> [reporting.ExtensionTest.ExtensionItem.item] {
>   childAttribute: "childtst"
> } 


*Now when i serialise and de-serialise the object as follows:*

ByteArrayOutputStream out = new ByteArrayOutputStream();
> ObjectOutputStream objOut = null;
> try {
>     objOut = new ObjectOutputStream(out);
>     objOut.writeObject(oldObj);
> }finally {
> out.close();
> }
> ExtensionTest newObj = null;
> ObjectInputStream objIn = null;
> try {
>     objIn = new ObjectInputStream(
>           new ByteArrayInputStream(out.toByteArray()));
>     newObj = (ExtensionTest)objIn.readObject();
> }finally {
>    out.close();
> }
> System.out.println("newObj: " + newObj);
> System.out.println("newObj.hasExtension: " + 
> newObj.hasExtension(ExtensionTest.ExtensionItem.item)); 


*This results in the following output:*

newObj: parentAttribute: "tst"
> 100: "\n\bchildtst"
> newObj.hasExtension: *false* 




Any idea why this is and how to avoid it? 

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to