fairyhawk / protostuff

Automatically exported from code.google.com/p/protostuff
Apache License 2.0
0 stars 0 forks source link

Serialize directly to ByteString #97

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I need to embed a protobuf message inside another protobuf message, but as the 
message class inside is variable, in the outer message I'm using a field of the 
"bytes" type to contain the inner one.

So in the process of serializing, I need to first serialize the inner message, 
and then set the byte array as a field in the outer one.

What's the problem? Currently I can only serialize to a byte[], and to set the 
"bytes" field, I need an instance of a ByteString object. But to create a new 
ByteString from a byte[], to ensure readonly-ness, it performs an extra 
arraycopy.

A simple solution would be to add a new method in 
ProtobufIOUtil/ProtostuffIOUtil to serialize to a ByteString, something like 
this will be very convenient (ByteString.wrap is package private):

    /**
     * Serializes the {@code message} into a {@code ByteString} using the given schema.
     * 
     * @return the ByteString containing the data.
     */
    public static <T> ByteString toByteString(T message, Schema<T> schema, LinkedBuffer buffer)
    {
        return ByteString.wrap(toByteArray(message, schema, buffer));
    }

Thanks,
Isaac

Original issue reported on code.google.com by icruz...@gmail.com on 2 Jan 2012 at 2:17

GoogleCodeExporter commented 8 years ago
Of course, the deserializing equivalent:

    /**
     * Merges the {@code message} with the {@code ByteString} using the given {@code schema}.
     */
    public static <T> void mergeFrom(ByteString data, T message, Schema<T> schema)
    {
        IOUtil.mergeFrom(data.getBytes(), 0, data.size(), message, schema, false);
    }

Original comment by icruz...@gmail.com on 2 Jan 2012 at 4:40

GoogleCodeExporter commented 8 years ago
Was your messages generated by java_bean? (I can add an option for it to have 
byte[] fields instead)
or do you prefer working with ByteString instead of byte[]?

The problem with adding that is, I have to add it to every single $FormatIOUtil 
w/c adds unnecessary stuff for others (the api is uniform for all formats).

What I suggest doing is to create a utility class in the package of the 
package-private stuff, and use it for my customization/optimization needs.
I do that all the time with other libs.  
That is the precise reason why I left them package-private so that they can 
still be accessed by 3rd-party devs who know what they're doing.

Original comment by david.yu...@gmail.com on 3 Jan 2012 at 2:17

GoogleCodeExporter commented 8 years ago
Yes, I'm using java_bean. An option to use byte[] fields would work too.

I can use an utility class in your package as well... A better option would be 
to extend some classes, but almost all of them are final.

Finally, what are your thoughts on implementing equals/hashcode in the 
generated beans? (probably worth another ticket though)

Original comment by icruz...@gmail.com on 3 Jan 2012 at 9:19

GoogleCodeExporter commented 8 years ago
All good points.  When I've time, I'll try to add a compiler option to use 
byte[] and generate equals/hashcode (feel free to create an issue for that).

Have you tried extending the java_bean template to generate your pojos?
You can checkout the wiki on howto, and here's a sample snip: 
http://groups.google.com/group/protostuff/browse_thread/thread/30bf61334f559878/
c17bd69a849f8e69?q=#1007bae44786f114

If you're adding extra functionality and would like to make it a compiler 
option for java_bean, I'll be glad to add it if it makes sense.

Original comment by david.yu...@gmail.com on 3 Jan 2012 at 12:05