You will need to provide the type during construction of your
instance.  You will also want to provide the Descriptor for the type
you want to deserialize in order to safely perform that step.

i.e.

public GPBFormat(Class<T> clazz, Descriptor d) { //save the type
information to instance variables }

Notably, you should not be using <T extends DynamicMessage> as nothing
extends DynamicMessage... you want <T extends Message> or <T extends
Message>.

Alternately you can just use the DynamicMessage type as intended (an
unknown message type) and not use any parameterization, but this gets
trickier to use as a developer as you cannot access fields with direct
getters - you need to use the FieldDescriptors to access fields.

PS:  I've written the class you're trying to write (a few times), and
I eventually went with not using parameterization at all but just
using Message and DynamicMessage directly and accessing fields using
FieldDescriptors.

On Feb 22, 2:00 pm, rameshr <rudra.ram...@gmail.com> wrote:
> Hi all im very new to google protocol buffers and java generics.
>  In my application in one class im serializing and deserializing
> objects using google protocl buffers and my class is parameterized by
> actual google protocol buffer class.
>  following is the my code.
>
>  public class GPBFormat<T extends DynamicMessage> implements
> IGPBFormat<T> {
>
>         @Override
>         public byte[] serialize(T t)
>         {
>           return t.toByteArray();
>         }
>
>       @Override
>       public T deSerialize( byte[] value)
>       {
>        //deSerialization implemenation.
>       }
> in above code serialization is working fine.but the problem with is
> deSerialization. in deSerialization method here i dont know about
> actual type of "T extends GeneratedMessage", without knowing concrete
> class im unable to deSerialize the byte array into google protocol
> buffer type.
>
> one approach i know is we can send concrete type of message to
> deSerialize method,but my app wont accept this style of code(becoz
> when i write code like that,that code will be google protocl buffer
> specific)
>
> so how can i solve this without sending actual type to deSerialize
> method.
> plz replay.

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