From a635302e9ae4a8ced135620e355697ccf2a27b52 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Mon, 4 Dec 2017 14:15:48 +0200 Subject: Added Tango.Serialization. Added Tango.Settings. --- .../Tango.Serialization/BinaryDataSerializer.cs | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Software/Visual_Studio/Tango.Serialization/BinaryDataSerializer.cs (limited to 'Software/Visual_Studio/Tango.Serialization/BinaryDataSerializer.cs') diff --git a/Software/Visual_Studio/Tango.Serialization/BinaryDataSerializer.cs b/Software/Visual_Studio/Tango.Serialization/BinaryDataSerializer.cs new file mode 100644 index 000000000..565854804 --- /dev/null +++ b/Software/Visual_Studio/Tango.Serialization/BinaryDataSerializer.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.Serialization.Formatters.Binary; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.Serialization +{ + /// + /// Represents a data serializer for serializing data using binary formatter. + /// + public class BinaryDataSerializer : 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); + } + } + + /// + /// 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) + { + BinaryFormatter f = new BinaryFormatter(); + f.Serialize(st, obj); + } + + /// + /// Deserialize object from stream. + /// + /// Type of object to deserialize. + /// Stream to read from. + /// The resulting object. + public T DeserializeFromStream(Stream st) + { + BinaryFormatter f = new BinaryFormatter(); + return (T)f.Deserialize(st); + } + + /// + /// Returns the serializer full name. + /// + /// + public override string ToString() + { + return SerializationHelper.GetSerializerName(this); + } + } +} -- cgit v1.3.1