Re: [protobuf] java parse with class known at runtime (and compiled proto)

2010-12-03 Thread Siju Varghese
Take a look at
https://github.com/sijuv/protobuf-codec/blob/master/src/main/java/com/google/protobuf/codec/Codec.java

https://github.com/sijuv/protobuf-codec/blob/master/src/main/java/com/google/protobuf/codec/AbstractCodec.java


https://github.com/sijuv/protobuf-codec is a library which I plan to extend
to support other serialization schemes, currently supports JSON.

Regards,
Siju

On Sat, Dec 4, 2010 at 2:04 AM, Evan Jones  wrote:

> On Dec 3, 2010, at 14:21 , Koert Kuipers wrote:
>
>> public class ProtobufDeserializer {
>>public T fromByteBuffer(ByteBuffer byteBuffer) {
>>
>
> I don't *think* the generic type is going to be enough due to erasure, but
> I'm not a generics expert. I know something like the following works (I may
> be messing up the generics syntax since I'm not super familiar with it):
>
>  public T fromByteBuffer(ByteBuffer byteBuffer, T
> defaultInstance) {
>  Builder b = defaultInstance.newBuilderForType();
>  b.mergeFrom(ByteString.copyFrom(byteBuffer));
>  return b.build();
> }
>
> You can get defaultInstance from ConcreteMessageType.getDefaultInstance();
>
> You may want to create a tiny InputStream wrapper around ByteBuffer to
> avoid an extra copy, or if you know it is a heap byte buffer, use the array
> mergeFrom().
>
> Hope that helps,
>
> Evan
>
> --
> Evan Jones
> http://evanjones.ca/
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To post to this group, send email to proto...@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.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to proto...@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.



Re: [protobuf] java parse with class known at runtime (and compiled proto)

2010-12-03 Thread Evan Jones

On Dec 3, 2010, at 14:21 , Koert Kuipers wrote:

public class ProtobufDeserializer {
public T fromByteBuffer(ByteBuffer byteBuffer) {


I don't *think* the generic type is going to be enough due to erasure,  
but I'm not a generics expert. I know something like the following  
works (I may be messing up the generics syntax since I'm not super  
familiar with it):


 public T fromByteBuffer(ByteBuffer byteBuffer,  
T defaultInstance) {

  Builder b = defaultInstance.newBuilderForType();
  b.mergeFrom(ByteString.copyFrom(byteBuffer));
  return b.build();
}

You can get defaultInstance from  
ConcreteMessageType.getDefaultInstance();


You may want to create a tiny InputStream wrapper around ByteBuffer to  
avoid an extra copy, or if you know it is a heap byte buffer, use the  
array mergeFrom().


Hope that helps,

Evan

--
Evan Jones
http://evanjones.ca/

--
You received this message because you are subscribed to the Google Groups "Protocol 
Buffers" group.
To post to this group, send email to proto...@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.



[protobuf] java parse with class known at runtime (and compiled proto)

2010-12-03 Thread Koert Kuipers
Hey all,

I found a bunch of threads on this topic already, but i cannot figure it
out.

This is my situation:
* proto files have been compiled and java classes for them are available
* i need to deserialize messages with different types
* the class of the message type is available at the time of deserialization
as a generic type

so i have a class with one function that looks like this:


import com.google.protobuf.Message

public class ProtobufDeserializer {
public T fromByteBuffer(ByteBuffer byteBuffer) {
   *** do something ***
}
}

Any ideas? Is Message  the wrong class/interface to extend from? I have not
been able to find any other class/interface where i can get it to work.

Thanks Koert

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to proto...@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.