using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml.Serialization;
namespace ChadSoft
{
///
/// Supported serialization methods
///
public enum SerializationMethod
{
///
/// Serialization to XML format (System.Xml.Serialization.XmlSerializer)
///
XML,
///
/// Serialization to Binary format
/// (System.Runtime.Serialization.Formatters.Binary.BinaryFormatter)
///
Binary
}
///
/// Serialization helper class used to make serialization more manageable
/// by removing the implementation details until the last possible call.
///
public static class Serializer
{
#region DefaultSerializationMethod
private static readonly SerializationMethod _DefaultSerializationMethod =
Parse.AppSetting("SerializationMethod");
///
/// Gets the default serialization method.
///
/// The default serialization method.
public static SerializationMethod DefaultSerializationMethod
{
get { return _DefaultSerializationMethod; }
}
#endregion
#region Serialize
#region To File
///
/// Serializes the source to a file
/// using the specified .
///
/// The target Type to serialize to
/// The object to be serialized.
/// Path to the file used to store the result of the serialization.
public static void Serialize(T source, string filename)
{
Serialize(DefaultSerializationMethod, source, filename);
}
///
/// Serializes the source to a file
/// using the specified .
///
/// The target Type to serialize to
/// The to use to serialize the source.
/// The object to be serialized.
/// Path to the file used to store the result of the serialization.
public static void Serialize(SerializationMethod method, T source, string filename)
{
using (FileStream fileStream = new FileStream(filename, FileMode.Create))
{
Serialize(method, source, fileStream);
}
}
#endregion
#region To String
///
/// Serializes the to a string using
/// the specified .
///
/// The target Type to serialize to
/// The source object.
/// String containing the serialized data of the
public static string Serialize(T source)
{
return Serialize(DefaultSerializationMethod, source);
}
///
/// Serializes the to a string using
/// the specified .
///
/// The target Type to serialize to
/// The to serialize with.
/// The source object.
/// String containing the serialized data of the
public static string Serialize(SerializationMethod method, T source)
{
string serializedSource;
using (MemoryStream stream = new MemoryStream())
{
Serialize(method, source, stream);
stream.Seek(0, SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(stream))
{
serializedSource = reader.ReadToEnd();
}
}
return serializedSource;
}
#endregion
#region To Stream
///
/// Serializes the source to the specified stream
/// using the specified .
///
/// The target Type to serialize to
/// The object to be serialized.
/// The stream to pipe the output of the serialization to.
public static void Serialize(T source, Stream stream)
{
Serialize(DefaultSerializationMethod, source, stream);
}
///
/// Serializes the source to the specified stream
/// using the specified .
///
/// The target Type to serialize to
/// The to use to serialize the source.
/// The object to be serialized.
/// The stream to pipe the output of the serialization to.
public static void Serialize(SerializationMethod method, T source, Stream stream)
{
switch (method)
{
case SerializationMethod.Binary:
SerializeBinary(source, stream);
break;
case SerializationMethod.XML:
SerializeXml(source, stream);
break;
default:
throw new NotSupportedException(String.Format(
"SerializationMethod {0} is not a supported serialization method!",
method));
}
}
#endregion
#endregion
#region Deserialize
#region From String
///
/// Deserializes the specified string containing serialized data.
///
/// The target Type to deserialize to
/// A string containing serialized data of the target type.
///
/// Deserialized object from
///
public static T Deserialize(string serializedData)
{
return Deserialize(DefaultSerializationMethod, serializedData);
}
///
/// Deserializes the specified string containing serialized data.
///
/// The target Type to deserialize to
///
/// The originally used to serialize the object.
///
/// A string containing serialized data of the target type.
///
/// Deserialized object from
///
public static T Deserialize(SerializationMethod method, string serializedData)
{
T retVal;
using(Stream stream = new MemoryStream(Encoding.Unicode.GetBytes(serializedData)))
{
stream.Flush();
retVal = Deserialize(method, stream);
}
return retVal;
}
#endregion
#region From Stream
///
/// Deserializes the specified stream.
///
/// The target Type to deserialize to
/// The stream.
/// Deserialized object from
public static T Deserialize(Stream stream)
{
return Deserialize(DefaultSerializationMethod, stream);
}
///
/// Deserializes the specified stream.
///
/// The target Type to deserialize to
///
/// The originally used to serialize the object.
///
/// The stream.
/// Deserialized object from
public static T Deserialize(SerializationMethod method, Stream stream)
{
switch (method)
{
case SerializationMethod.Binary:
return DeserializeBinary(stream);
case SerializationMethod.XML:
return DeserializeXml(stream);
default:
throw new NotSupportedException(String.Format(
"SerializationMethod {0} is not a supported deserialization method!",
method));
}
}
#endregion
#endregion
#region Helpers
#region Binary Serialization Helpers
private static void SerializeBinary(object target, Stream stream)
{
new BinaryFormatter().Serialize(stream, target);
}
private static T DeserializeBinary(Stream stream)
{
return (T)(new BinaryFormatter().Deserialize(stream));
}
#endregion
#region XML Serialization Helpers
private static void SerializeXml(T target, Stream stream)
{
new XmlSerializer(typeof(T)).Serialize(stream, target);
}
private static T DeserializeXml(Stream stream)
{
return (T)new XmlSerializer(typeof(T)).Deserialize(stream);
}
#endregion
#endregion
}
}