I hope this post might help other people in the same scenario. I've been trying to use protocol buffers to save and read plain objects to binary files and to be platform / language independent.
- Protobuf-net is the serializer adopted on the .net side and
- Protostuff is the serializer used on the java side
Everything works fine when serializing / deserializing in one platform, but it doesn't work if I try to read a binary file created 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 the pojo is:
import io.protostuff.Tag;
public final class PocoDto {
@Tag(1)
public String name;
@Tag(2)
public int timestamp;
}
The method for serializing in .net using protobuf-net is:
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();
}
}
The method used for serializing in java using protostuff is:
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 deserializing:
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);
}
By comparing the two binary files created in each case, it seems that the one in C# always append a newline after every entry, while this does not happen with the file generated in java. Moreover, creating a list with protostuff add different delimiters and it does not seem to handle the lenght prefix in the same manner. I am just wondering if I am choosing the wrong options when I set up the buffers or if the two output are really not compatible. Thank you for your help