Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary serialization and deserialization without creating files (via strings)

Tags:

I'm trying to create a class that will contain functions for serializing/deserializing objects to/from string. That's what it looks like now:

public class BinarySerialization     {         public static string SerializeObject(object o)         {             string result = "";              if ((o.GetType().Attributes & TypeAttributes.Serializable) == TypeAttributes.Serializable)             {                 BinaryFormatter f = new BinaryFormatter();                 using (MemoryStream str = new MemoryStream())                 {                     f.Serialize(str, o);                     str.Position = 0;                      StreamReader reader = new StreamReader(str);                     result = reader.ReadToEnd();                 }             }              return result;         }          public static object DeserializeObject(string str)         {             object result = null;              byte[] bytes = System.Text.Encoding.ASCII.GetBytes(str);             using (MemoryStream stream = new MemoryStream(bytes))             {                 BinaryFormatter bf = new BinaryFormatter();                 result = bf.Deserialize(stream);             }              return result;         }     } 

SerializeObject method works well, but DeserializeObject does not. I always get an exception with message "End of Stream encountered before parsing was completed". What may be wrong here?

like image 744
the_V Avatar asked May 18 '10 22:05

the_V


People also ask

How does binary serialization work?

Binary serialization converts objects into binary information. This gives a concise result and ensures that when the data is deserialized, the object structure is correctly reconstructed. Deserialized objects are of the same types as the originating objects and references are recreated.

What is the difference between binary serialization and XML serialization?

Xml Serializer serializes only public member of object but Binary Serializer serializes all member whether public or private. In Xml Serialization, some of object state is only saved but in Binary Serialization, entire object state is saved.

What is serialization and deserialization of binary tree?

Serialization is to store tree in a file so that it can be later restored. The structure of tree must be maintained. Deserialization is reading tree back from file. Recommended Practice. Serialize and Deserialize a Binary Tree.

Which class is used for binary serialization and deserialization?

Code Explanation:- First, we create an object of the Tutorial class. We then assign the value of “1” to ID and “. net” to the name property. We then use the formatter class which is used to serialize or convert the object to a binary format.


1 Answers

The result of serializing an object with BinaryFormatter is an octet stream, not a string.
You can't just treat the bytes as characters like in C or Python.

Encode the serialized object with Base64 to get a string instead:

public static string SerializeObject(object o) {     if (!o.GetType().IsSerializable)     {         return null;     }      using (MemoryStream stream = new MemoryStream())     {         new BinaryFormatter().Serialize(stream, o);         return Convert.ToBase64String(stream.ToArray());     } } 

and

public static object DeserializeObject(string str) {     byte[] bytes = Convert.FromBase64String(str);      using (MemoryStream stream = new MemoryStream(bytes))     {         return new BinaryFormatter().Deserialize(stream);     } } 
like image 142
dtb Avatar answered Sep 28 '22 11:09

dtb