On Thu, Apr 19, 2012 at 11:36 AM, kandji <adram...@googlemail.com> wrote:

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

> 2- I would like to serialize an instance of the class
> "MyComplexClass.java".

> As you can see, I would use "MyComplexClass" as a field type in the
> message descritption.  Is that possible? Or can I only use scalar/
> primitive types that are built-in in java?

No, you can't do that directly.
You have a few options:

1) make MyComplexClass serializable, make the field type 'bytes', and
stash the serialized form of the instance as a bytearray. This defeats
the purpose of using protobuf, though - you lose interoperability with
other languages, you are relying on Java's serialization rules for
compatibility, and you have a speed penalty from using Java
serialization.

2) create a separate protobuf message type for MyComplexClass, and so
on for its fields until you reach primitive types that can be directly
represented; then build some glue code to convert between the
generated message classes and MyComplexClass.

3) if MyComplexClass is really just a data-holding class, you might
want to consider using the message classes generated by step (2)
directly, replacing MyComplexClass entirely.

If you have many classes to apply (2) to, and they have a regular
structure, then you could write a tool to automate the process of
generating message definitions and glue code from classfiles.

Oliver

-- 
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.

Reply via email to