Protocol Buffers from poco to pojo and pojo to poco

I hope this post can help other people in the same scenario. I am trying to use protocol buffers to store and read simple objects in binaries and is platform / language independent.

  • Protobuf-net is a serializer adopted on the .net side and
  • Prototype is the serializer used on the java side

Everything works fine when serializing / deserializing on the same platform, but it doesn't work if I try to read a binary generated in .net from the Java platform and vice versa.

In C #, I have the following poco:

[ProtoContract]
public class PocoDto
{
    [ProtoMember(1)]
    public string name { get; set; }
    [ProtoMember(2)]
    public int timestamp { get; set; }
}

      

In java pojo:

import io.protostuff.Tag;

public final class PocoDto {
    @Tag(1)
    public String name;
    @Tag(2)
    public int timestamp;
}

      

Serialization method in .net using protobuf-net:

    internal static void AppendToBin(PocoDto poco, string path)
    {
        using (var fs = new FileStream(path, FileMode.Append))
        {
            Serializer.SerializeWithLengthPrefix(fs, poco, PrefixStyle.Base128, 1);
        }         
    }

      

while the method I am using to deserialize is

    public static List<PocoDto> FromBin(this string path)
    {
        using (var fs = new FileStream(path, FileMode.Open))
        {
           return Serializer.DeserializeItems<T>(fs, PrefixStyle.Base128, 1).ToList();
        }
    }

      

Method used to serialize in java using prototype:

public static void append(PocoDto pojo, String filePath) throws IOException
{
    Schema<PocoDto> schema = RuntimeSchema.getSchema(PocoDto.Class);
    File file = new File(filePath);
    file.createNewFile();       
    FileOutputStream out = new FileOutputStream(file, true);    
    LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);

    ProtostuffIOUtil.writeDelimitedTo(out, pojo, schema, buffer);
    buffer.clear();
    out.flush();
    out.close();
}

      

And for deserialization:

public static List<PocoDto> fromBin(String filePath) throws IOException
{
    Schema<PocoDto> schema = RuntimeSchema.getSchema(pojoType);
    File file = new File(filePath);
    if(!file.exists())
    {
        return null;
    }
    FileInputStream inStream = new FileInputStream(new File(filePath)); 
    return ProtobufIOUtil.parseListFrom(inStream, schema);  
}

      

Comparing the two binaries generated in each case, it seems that in C # a newline is always added after each entry, while this does not happen with a file generated in java. Moreover, creating a prototyped list adds different delimiters and doesn't seem to handle the length prefix in the same way. I'm just wondering if I'm picking the wrong options when setting up the buffers or if the two outputs are really incompatible. Thank you for your help.

+3


source to share





All Articles