Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Protocol Buffers - protobuf-net deserialization not working properly

I am experiencing difficulties successfully deserializing a byte array that had been serialized with a pre-compiled protobuff serializer class ModelSerializer. Using this, the serialization and deserialization of classes within my defined data model MyData works when I serialize to a file and deserialize from the file to MyData.

However, I have another requirement, which is to serialize MyData to a byte array, and deserialize the byte array to MyData. Below is the basic class called MyDataConverter that has 2 static methods, one to convert MyData to the byte array and the other to convert a byte array to MyData.

I could serialize MyData to a byte array successfully and write it onto a MemoryStream. However, when I deserialize the byte array, I do get a non-null instance of MyData, but all the data in it is lost (zero default, or null values for custom types).

Could I have gone wrong here? Note that I did use similar code to successfully serialize to a file and deserialize from a file, so it probably isnt something wrong with MyModel nor the pre compiled ModelSerializer. But I could paste some of that info here if necessary.

public class MyDataConverter
{
    public static byte [] MyDataToBytes (MyData myData)
    {
        MemoryStream stream = new MemoryStream();
        ModelSerializer serializer = new ModelSerializer();

        serializer.Serialize (stream, myData);
        byte [] bytes = stream.ToArray();
        Stream.Close();

        return bytes;
    }

    public static MyData BytesToMyData (byte [] bytes)
    {
        MyData myData = null;

        MemoryStream stream = new MemoryStream();
        stream.Write (bytes, 0, bytes.Length);

        ModelSerializer serializer = new ModelSerializer();

        myData = (MyData) serializer.Deserialize (stream, myData, typeof (MyData));
        stream.Close();

        return myData;
    }
}
like image 948
Nicholas Ang Avatar asked Jan 15 '23 03:01

Nicholas Ang


1 Answers

This is the problem:

MemoryStream stream = new MemoryStream();
stream.Write (bytes, 0, bytes.Length);

ModelSerializer serializer = new ModelSerializer();
myData = (MyData) serializer.Deserialize (stream, myData, typeof (MyData));

When you try to deserialize, you're still at the end of the stream. You could just seek to the beginning afterwards, but it would be simpler to change the code to pass the byte array to the constructor:

MemoryStream stream = new MemoryStream(bytes, false);        
ModelSerializer serializer = new ModelSerializer();
myData = (MyData) serializer.Deserialize (stream, myData, typeof (MyData));
like image 170
Jon Skeet Avatar answered Jan 21 '23 18:01

Jon Skeet