using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; namespace Tango.Serialization { /// /// Represents a data serializer for serializing data using standard xml serializer. /// public class XmlDataSerializer : IDataSerializer { /// /// Serialize object to a file. /// /// Type of specified object. /// The specified object. /// The full path to the file to write. public void SerializeToFile(T obj, string filePath) { using (FileStream fs = new FileStream(filePath, FileMode.Create)) { SerializeToStream(obj, fs); } } /// /// Serialize object to encrypted file. /// /// Type of specified object. /// The specified object. /// The full path to the file to write. public void SerializeToEncryptedFile(T obj, string filePath) { var plainText = SerializeToString(obj); var cypherText = CryptographyHelper.Encrypt(plainText); File.WriteAllText(filePath, cypherText); } /// /// Serialize object to encrypted string. /// /// Type of specified object. /// The specified object. public String SerializeToEncryptedString(T obj) { var plainText = SerializeToString(obj); var cypherText = CryptographyHelper.Encrypt(plainText); return cypherText; } /// /// Deserialize object from encrypted file. /// /// Type of object to deserialize. /// The full path of the data file. /// The resulting object. public T DeserializeFromEncryptedFile(string filePath) { var cypherText = File.ReadAllText(filePath); var plainText = CryptographyHelper.Decrypt(cypherText); return DeserializeFromString(plainText); } /// /// Deserialize object from encrypted string. /// /// Type of object to deserialize. /// The resulting object. public T DeserializeFromEncryptedString(string encyptedString) { var plainText = CryptographyHelper.Decrypt(encyptedString); return DeserializeFromString(plainText); } /// /// Deserialize object from a file. /// /// Type of object to deserialize. /// The full path of the data file. /// The resulting object. public T DeserializeFromFile(string filePath) { using (FileStream fs = new FileStream(filePath, FileMode.Open)) { return DeserializeFromStream(fs); } } /// /// Serialize object to stream. /// /// Type of specified object. /// The specified object. /// The stream to write. public void SerializeToStream(T obj, Stream st) { XmlSerializer f = XmlSerializer.FromTypes(new[] { typeof(T) })[0]; //Microsoft bug workaround. f.Serialize(st, obj); } /// /// Serialize object to string. /// /// Type of specified object. /// The specified object. /// public String SerializeToString(T obj) { XmlSerializer f = XmlSerializer.FromTypes(new[] { typeof(T) })[0]; //Microsoft bug workaround. using (StringWriter textWriter = new StringWriter()) { f.Serialize(textWriter, obj); return textWriter.ToString(); } } /// /// Deserialize object from stream. /// /// Type of object to deserialize. /// Stream to read from. /// The resulting object. public T DeserializeFromStream(Stream st) { XmlSerializer f = XmlSerializer.FromTypes(new[] { typeof(T) })[0]; //Microsoft bug workaround. return (T)f.Deserialize(st); } /// /// Deserialize object from string. /// /// Type of specified object. /// The specified object. /// public T DeserializeFromString(String str) { XmlSerializer f = XmlSerializer.FromTypes(new[] { typeof(T) })[0]; //Microsoft bug workaround. using (TextReader reader = new StringReader(str)) { return (T)f.Deserialize(reader); } } /// /// Returns the serializer full name. /// /// public override string ToString() { return SerializationHelper.GetSerializerName(this); } } }