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;
}
}