If you are writing a library, and the client of your library knows the exact
type desired, then they should pass to you the class's default instance (C++
and Java; in Python they might as well pass the class itself). In C++ this
is MyType::default_instance(), in Java MyType.getDefaultInstance(). Given
the default instance, you can call various methods on the interface to do
everything you need. E.g. to parse:
C++:
Message* instance = default_instance->New();
instance->ParseFromString(data);
Java:
Message.Builder builder =
defaultInstance.newBuilderForType().mergeFrom(data);
if (!builder.isInitialized()) {
return false;
}
Message instance = builder.build();
On Tue, Jul 13, 2010 at 1:38 AM, Galder Zamarreno
<[email protected]>wrote:
> Hi,
>
> I want to use protobuf as a generic marshalling mechanism and I'm
> having some issues with the unmarshalling part, because I need
> knowledge of what needs to be unmarshalled, i.e. in Java, access to
> static parseFrom method in target class, which makes it hard to use it
> in a generic way. A hacky workaround for the java world would be to
> write the class name as UTF-8 string and after instantiating it in the
> reading part, use reflection. Obvioulsy, this would only work for the
> java world. Any ideas of similar solutions for the Python/C++ world?
>
> I've also read about self-describing messages but that API is only
> available for C++ and Java and not Python, so it's not enough.
>
> Cheers,
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<protobuf%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/protobuf?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/protobuf?hl=en.