October 11, 2010

C# Extension Methods to Serialize and Deserialize any object

I don’t know if that has lots of usages where you come from, but felt like it was a nice thing to have and thought of sharing it with you

I needed a method that can serialize and object that I can create and deserialize any string that represents any type, so ended up with the following extension methods:

/// <summary>
/// Serializes the specified obj.
/// </summary>
/// <param name="obj">The obj.</param>
/// <returns>A string representing serialized data</returns>
public static string Serialize(this object obj)
{
    //Check is object is serializable before trying to serialize
    if (obj.GetType().IsSerializable)
    {
        using (var stream = new MemoryStream())
        {
            var serializer = new XmlSerializer(obj.GetType());
            serializer.Serialize(stream, obj);
            var bytes = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(bytes, 0, bytes.Length);
 
            return Encoding.UTF8.GetString(bytes);
        }
    }
    throw new NotSupportedException(string.Format("{0} is not serializable.", obj.GetType()));
}
 
/// <summary>
/// Deserializes the specified serialized data.
/// </summary>
/// <param name="serializedData">The serialized data.</param>
/// <returns></returns>
public static T Deserialize<T>(this string serializedData)
{
    var serializer = new XmlSerializer(typeof (T));
    var reader = new XmlTextReader(new StringReader(serializedData));
 
    return (T) serializer.Deserialize(reader);
}

Happy Coding!

No comments:

Post a Comment