views:

59

answers:

2

Hi,

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?

A: 

Use UTF8 Base64 encoding instead of ASCII, for both encoding and decoding.

Stephen Cleary
Not every octet stream is a valid UTF-8 encoded string.
dtb
Good catch; you are correct.
Stephen Cleary
+3  A: 

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);
    }
}
dtb
that's great, thanks
the_V
@Pindatjuh: That is incorrect. Base64 encodes 6 bits per character, which means that you'll generate 8/6 -- approximately 1.33 -- bytes of output for each byte of input. ASCII encodes 7 bits per character, which means that you'll generate 8/7 -- approximately 1.14 -- output bytes per input byte. (Assuming here, optimistically, that you're using the full 7-bit range. Using only 92 characters, as you propose, would be slightly less efficient.)
LukeH