aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Serialization
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2017-12-04 14:15:48 +0200
committerRoy Ben-Shabat <Roy@Twine-s.com>2017-12-04 14:15:48 +0200
commita635302e9ae4a8ced135620e355697ccf2a27b52 (patch)
tree47b0cdebe8958833f45822970117160ce6c9d237 /Software/Visual_Studio/Tango.Serialization
parentc47075cc333329fc6bc93679d847cadcb050436f (diff)
downloadTango-a635302e9ae4a8ced135620e355697ccf2a27b52.tar.gz
Tango-a635302e9ae4a8ced135620e355697ccf2a27b52.zip
Added Tango.Serialization.
Added Tango.Settings.
Diffstat (limited to 'Software/Visual_Studio/Tango.Serialization')
-rw-r--r--Software/Visual_Studio/Tango.Serialization/BinaryDataSerializer.cs77
-rw-r--r--Software/Visual_Studio/Tango.Serialization/CryptographyHelper.cs156
-rw-r--r--Software/Visual_Studio/Tango.Serialization/IDataSerializer.cs47
-rw-r--r--Software/Visual_Studio/Tango.Serialization/JsonDataSerializer.cs87
-rw-r--r--Software/Visual_Studio/Tango.Serialization/Properties/AssemblyInfo.cs6
-rw-r--r--Software/Visual_Studio/Tango.Serialization/SerializationHelper.cs66
-rw-r--r--Software/Visual_Studio/Tango.Serialization/Tango.Serialization.csproj64
-rw-r--r--Software/Visual_Studio/Tango.Serialization/XmlDataSerializer.cs161
-rw-r--r--Software/Visual_Studio/Tango.Serialization/packages.config5
9 files changed, 669 insertions, 0 deletions
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
+{
+ /// <summary>
+ /// Represents a data serializer for serializing data using binary formatter.
+ /// </summary>
+ public class BinaryDataSerializer : IDataSerializer
+ {
+ /// <summary>
+ /// Serialize object to a file.
+ /// </summary>
+ /// <typeparam name="T">Type of specified object.</typeparam>
+ /// <param name="obj">The specified object.</param>
+ /// <param name="filePath">The full path to the file to write.</param>
+ public void SerializeToFile<T>(T obj, string filePath)
+ {
+ using (FileStream fs = new FileStream(filePath,FileMode.Create))
+ {
+ SerializeToStream(obj, fs);
+ }
+ }
+
+ /// <summary>
+ /// Deserialize object from a file.
+ /// </summary>
+ /// <typeparam name="T">Type of object to deserialize.</typeparam>
+ /// <param name="filePath">The full path of the data file.</param>
+ /// <returns>The resulting object.</returns>
+ public T DeserializeFromFile<T>(string filePath)
+ {
+ using (FileStream fs = new FileStream(filePath, FileMode.Open))
+ {
+ return DeserializeFromStream<T>(fs);
+ }
+ }
+
+ /// <summary>
+ /// Serialize object to stream.
+ /// </summary>
+ /// <typeparam name="T">Type of specified object.</typeparam>
+ /// <param name="obj">The specified object.</param>
+ /// <param name="st">The stream to write.</param>
+ public void SerializeToStream<T>(T obj, Stream st)
+ {
+ BinaryFormatter f = new BinaryFormatter();
+ f.Serialize(st, obj);
+ }
+
+ /// <summary>
+ /// Deserialize object from stream.
+ /// </summary>
+ /// <typeparam name="T">Type of object to deserialize.</typeparam>
+ /// <param name="st">Stream to read from.</param>
+ /// <returns>The resulting object.</returns>
+ public T DeserializeFromStream<T>(Stream st)
+ {
+ BinaryFormatter f = new BinaryFormatter();
+ return (T)f.Deserialize(st);
+ }
+
+ /// <summary>
+ /// Returns the serializer full name.
+ /// </summary>
+ /// <returns></returns>
+ public override string ToString()
+ {
+ return SerializationHelper.GetSerializerName(this);
+ }
+ }
+}
diff --git a/Software/Visual_Studio/Tango.Serialization/CryptographyHelper.cs b/Software/Visual_Studio/Tango.Serialization/CryptographyHelper.cs
new file mode 100644
index 000000000..e072827fe
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Serialization/CryptographyHelper.cs
@@ -0,0 +1,156 @@
+using System;
+using System.Text;
+using System.Security.Cryptography;
+using System.IO;
+using System.Linq;
+using System.Security;
+using System.Runtime.InteropServices;
+
+namespace Tango.Serialization
+{
+ /// <summary>
+ /// Represents a helper class for encryption and decryption of plain text.
+ /// </summary>
+ internal static class CryptographyHelper
+ {
+ private static SecureString passkey;
+
+ /// <summary>
+ /// Initializes the <see cref="CryptographyHelper"/> class.
+ /// </summary>
+ static CryptographyHelper()
+ {
+ passkey = new SecureString();
+ passkey.AppendChar('S');
+ passkey.AppendChar('@');
+ passkey.AppendChar('Y');
+ passkey.AppendChar('!');
+ }
+
+ // This constant is used to determine the keysize of the encryption algorithm in bits.
+ // We divide this by 8 within the code below to get the equivalent number of bytes.
+ private const int Keysize = 256;
+
+ // This constant determines the number of iterations for the password bytes generation function.
+ private const int DerivationIterations = 1000;
+
+ /// <summary>
+ /// Encrypts the specified plain text.
+ /// </summary>
+ /// <param name="plainText">The plain text.</param>
+ /// <returns></returns>
+ public static string Encrypt(string plainText)
+ {
+ // Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
+ // so that the same Salt and IV values can be used when decrypting.
+ var saltStringBytes = Generate256BitsOfRandomEntropy();
+ var ivStringBytes = Generate256BitsOfRandomEntropy();
+ var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
+ using (var password = new Rfc2898DeriveBytes(ConvertSecureStringToString(passkey), saltStringBytes, DerivationIterations))
+ {
+ var keyBytes = password.GetBytes(Keysize / 8);
+ using (var symmetricKey = new RijndaelManaged())
+ {
+ symmetricKey.BlockSize = 256;
+ symmetricKey.Mode = CipherMode.CBC;
+ symmetricKey.Padding = PaddingMode.PKCS7;
+ using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
+ {
+ using (var memoryStream = new MemoryStream())
+ {
+ using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
+ {
+ cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
+ cryptoStream.FlushFinalBlock();
+ // Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
+ var cipherTextBytes = saltStringBytes;
+ cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
+ cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
+ memoryStream.Close();
+ cryptoStream.Close();
+ return Convert.ToBase64String(cipherTextBytes);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ /// <summary>
+ /// Decrypts the specified cipher text.
+ /// </summary>
+ /// <param name="cipherText">The cipher text.</param>
+ /// <returns></returns>
+ public static string Decrypt(string cipherText)
+ {
+ // Get the complete stream of bytes that represent:
+ // [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
+ var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
+ // Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
+ var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
+ // Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes.
+ var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
+ // Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string.
+ var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();
+
+ using (var password = new Rfc2898DeriveBytes(ConvertSecureStringToString(passkey), saltStringBytes, DerivationIterations))
+ {
+ var keyBytes = password.GetBytes(Keysize / 8);
+ using (var symmetricKey = new RijndaelManaged())
+ {
+ symmetricKey.BlockSize = 256;
+ symmetricKey.Mode = CipherMode.CBC;
+ symmetricKey.Padding = PaddingMode.PKCS7;
+ using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes))
+ {
+ using (var memoryStream = new MemoryStream(cipherTextBytes))
+ {
+ using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
+ {
+ var plainTextBytes = new byte[cipherTextBytes.Length];
+ var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
+ memoryStream.Close();
+ cryptoStream.Close();
+ return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ /// <summary>
+ /// Generate256s the bits of random entropy.
+ /// </summary>
+ /// <returns></returns>
+ private static byte[] Generate256BitsOfRandomEntropy()
+ {
+ var randomBytes = new byte[32]; // 32 Bytes will give us 256 bits.
+ using (var rngCsp = new RNGCryptoServiceProvider())
+ {
+ // Fill the array with cryptographically secure random bytes.
+ rngCsp.GetBytes(randomBytes);
+ }
+ return randomBytes;
+ }
+
+ /// <summary>
+ /// Converts the secure string to string.
+ /// </summary>
+ /// <param name="value">The value.</param>
+ /// <returns></returns>
+ private static String ConvertSecureStringToString(SecureString value)
+ {
+ IntPtr valuePtr = IntPtr.Zero;
+ try
+ {
+ valuePtr = Marshal.SecureStringToGlobalAllocUnicode(value);
+ return Marshal.PtrToStringUni(valuePtr);
+ }
+ finally
+ {
+ Marshal.ZeroFreeGlobalAllocUnicode(valuePtr);
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/Software/Visual_Studio/Tango.Serialization/IDataSerializer.cs b/Software/Visual_Studio/Tango.Serialization/IDataSerializer.cs
new file mode 100644
index 000000000..93465967e
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Serialization/IDataSerializer.cs
@@ -0,0 +1,47 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.Serialization
+{
+ /// <summary>
+ /// Represent an interface for creating a custom data serializer.
+ /// </summary>
+ public interface IDataSerializer
+ {
+ /// <summary>
+ /// Serialize object to a file.
+ /// </summary>
+ /// <typeparam name="T">Type of specified object.</typeparam>
+ /// <param name="obj">The specified object.</param>
+ /// <param name="filePath">The full path to the file to write.</param>
+ void SerializeToFile<T>(T obj, String filePath);
+
+ /// <summary>
+ /// Deserialize object from a file.
+ /// </summary>
+ /// <typeparam name="T">Type of object to deserialize.</typeparam>
+ /// <param name="filePath">The full path of the data file.</param>
+ /// <returns>The resulting object.</returns>
+ T DeserializeFromFile<T>(String filePath);
+
+ /// <summary>
+ /// Serialize object to stream.
+ /// </summary>
+ /// <typeparam name="T">Type of specified object.</typeparam>
+ /// <param name="obj">The specified object.</param>
+ /// <param name="st">The stream to write.</param>
+ void SerializeToStream<T>(T obj, Stream st);
+
+ /// <summary>
+ /// Deserialize object from stream.
+ /// </summary>
+ /// <typeparam name="T">Type of object to deserialize.</typeparam>
+ /// <param name="st">Stream to read from.</param>
+ /// <returns>The resulting object.</returns>
+ T DeserializeFromStream<T>(Stream st);
+ }
+}
diff --git a/Software/Visual_Studio/Tango.Serialization/JsonDataSerializer.cs b/Software/Visual_Studio/Tango.Serialization/JsonDataSerializer.cs
new file mode 100644
index 000000000..674f68289
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Serialization/JsonDataSerializer.cs
@@ -0,0 +1,87 @@
+using Newtonsoft.Json;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.Serialization
+{
+ /// <summary>
+ /// Represents a data serializer for serializing data using the light weight textual Json format.
+ /// </summary>
+ public class JsonDataSerializer : IDataSerializer
+ {
+
+ /// <summary>
+ /// Serialize object to a file.
+ /// </summary>
+ /// <typeparam name="T">Type of specified object.</typeparam>
+ /// <param name="obj">The specified object.</param>
+ /// <param name="filePath">The full path to the file to write.</param>
+ public void SerializeToFile<T>(T obj, string filePath)
+ {
+ using (FileStream fs = new FileStream(filePath, FileMode.Create))
+ {
+ SerializeToStream<T>(obj, fs);
+ }
+ }
+
+ /// <summary>
+ /// Deserialize object from a file.
+ /// </summary>
+ /// <typeparam name="T">Type of object to deserialize.</typeparam>
+ /// <param name="filePath">The full path of the data file.</param>
+ /// <returns>The resulting object.</returns>
+ public T DeserializeFromFile<T>(string filePath)
+ {
+ using (FileStream fs = new FileStream(filePath, FileMode.Open))
+ {
+ return DeserializeFromStream<T>(fs);
+ }
+ }
+
+ /// <summary>
+ /// Serialize object to stream.
+ /// </summary>
+ /// <typeparam name="T">Type of specified object.</typeparam>
+ /// <param name="obj">The specified object.</param>
+ /// <param name="st">The stream to write.</param>
+ public void SerializeToStream<T>(T obj, Stream st)
+ {
+ JsonSerializer serializer = new JsonSerializer();
+ using (StreamWriter streamReader = new StreamWriter(st))
+ {
+ serializer.Serialize(streamReader, obj);
+ }
+ }
+
+ /// <summary>
+ /// Deserialize object from stream.
+ /// </summary>
+ /// <typeparam name="T">Type of object to deserialize.</typeparam>
+ /// <param name="st">Stream to read from.</param>
+ /// <returns>The resulting object.</returns>
+ public T DeserializeFromStream<T>(Stream st)
+ {
+ JsonSerializer serializer = new JsonSerializer();
+ T data;
+ using (StreamReader streamReader = new StreamReader(st))
+ {
+ data = (T)serializer.Deserialize(streamReader, typeof(T));
+ }
+ return data;
+ }
+
+ /// <summary>
+ /// Returns the serializer full name.
+ /// </summary>
+ /// <returns></returns>
+ public override string ToString()
+ {
+ return SerializationHelper.GetSerializerName(this);
+ }
+
+ }
+}
diff --git a/Software/Visual_Studio/Tango.Serialization/Properties/AssemblyInfo.cs b/Software/Visual_Studio/Tango.Serialization/Properties/AssemblyInfo.cs
new file mode 100644
index 000000000..f3c038f0e
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Serialization/Properties/AssemblyInfo.cs
@@ -0,0 +1,6 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+[assembly: AssemblyTitle("Tango - Serialization Components")]
+[assembly: ComVisible(false)] \ No newline at end of file
diff --git a/Software/Visual_Studio/Tango.Serialization/SerializationHelper.cs b/Software/Visual_Studio/Tango.Serialization/SerializationHelper.cs
new file mode 100644
index 000000000..2d00c8621
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Serialization/SerializationHelper.cs
@@ -0,0 +1,66 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.Serialization
+{
+ /// <summary>
+ /// Contains a collection of IDataSerializer related helper methods.
+ /// </summary>
+ public static class SerializationHelper
+ {
+ /// <summary>
+ /// Gets a list of all IDataSerializer implemented types.
+ /// </summary>
+ /// <returns></returns>
+ public static List<Type> GetSerializersTypes()
+ {
+ var type = typeof(IDataSerializer);
+ var serializers = System.Reflection.Assembly.GetAssembly(type).GetTypes().Where(x => type.IsAssignableFrom(x) && !x.IsInterface);
+ return serializers.ToList();
+ }
+
+ /// <summary>
+ /// Gets a list of all IDataSerializer implemented instances.
+ /// </summary>
+ /// <returns></returns>
+ public static List<IDataSerializer> GetSerializersInstances()
+ {
+ var types = GetSerializersTypes();
+
+ List<IDataSerializer> serializers = new List<IDataSerializer>();
+
+ foreach (var type in types)
+ {
+ var instance = Activator.CreateInstance(type) as IDataSerializer;
+ serializers.Add(instance);
+ }
+
+ return serializers;
+ }
+
+ /// <summary>
+ /// Returns a serializer instance by type name.
+ /// </summary>
+ /// <param name="name">Name of IDataSerializer type.</param>
+ /// <returns></returns>
+ public static IDataSerializer GetSerializerByName(String name)
+ {
+ var types = GetSerializersTypes();
+ var type = types.SingleOrDefault(x => x.Name == name);
+ return Activator.CreateInstance(type) as IDataSerializer;
+ }
+
+ /// <summary>
+ /// Returns the name of the specified IDataSerializer.
+ /// </summary>
+ /// <param name="serializer">The specified IDataSerializer.</param>
+ /// <returns></returns>
+ public static String GetSerializerName(IDataSerializer serializer)
+ {
+ return serializer.GetType().Name;
+ }
+ }
+}
diff --git a/Software/Visual_Studio/Tango.Serialization/Tango.Serialization.csproj b/Software/Visual_Studio/Tango.Serialization/Tango.Serialization.csproj
new file mode 100644
index 000000000..453d317f5
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Serialization/Tango.Serialization.csproj
@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{22F87980-E990-4686-BE81-BE63D562C4D5}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Tango.Serialization</RootNamespace>
+ <AssemblyName>Tango.Serialization</AssemblyName>
+ <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="Google.Protobuf, Version=3.5.0.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604, processorArchitecture=MSIL">
+ <HintPath>..\packages\Google.Protobuf.3.5.0\lib\net45\Google.Protobuf.dll</HintPath>
+ </Reference>
+ <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
+ <HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
+ </Reference>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Net.Http" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="..\Versioning\GlobalVersionInfo.cs">
+ <Link>GlobalVersionInfo.cs</Link>
+ </Compile>
+ <Compile Include="BinaryDataSerializer.cs" />
+ <Compile Include="CryptographyHelper.cs" />
+ <Compile Include="IDataSerializer.cs" />
+ <Compile Include="JsonDataSerializer.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="SerializationHelper.cs" />
+ <Compile Include="XmlDataSerializer.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="packages.config" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+</Project> \ No newline at end of file
diff --git a/Software/Visual_Studio/Tango.Serialization/XmlDataSerializer.cs b/Software/Visual_Studio/Tango.Serialization/XmlDataSerializer.cs
new file mode 100644
index 000000000..0453e9120
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Serialization/XmlDataSerializer.cs
@@ -0,0 +1,161 @@
+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
+{
+ /// <summary>
+ /// Represents a data serializer for serializing data using standard xml serializer.
+ /// </summary>
+ public class XmlDataSerializer : IDataSerializer
+ {
+
+ /// <summary>
+ /// Serialize object to a file.
+ /// </summary>
+ /// <typeparam name="T">Type of specified object.</typeparam>
+ /// <param name="obj">The specified object.</param>
+ /// <param name="filePath">The full path to the file to write.</param>
+ public void SerializeToFile<T>(T obj, string filePath)
+ {
+ using (FileStream fs = new FileStream(filePath, FileMode.Create))
+ {
+ SerializeToStream<T>(obj, fs);
+ }
+ }
+
+ /// <summary>
+ /// Serialize object to encrypted file.
+ /// </summary>
+ /// <typeparam name="T">Type of specified object.</typeparam>
+ /// <param name="obj">The specified object.</param>
+ /// <param name="filePath">The full path to the file to write.</param>
+ public void SerializeToEncryptedFile<T>(T obj, string filePath)
+ {
+ var plainText = SerializeToString(obj);
+ var cypherText = CryptographyHelper.Encrypt(plainText);
+ File.WriteAllText(filePath, cypherText);
+ }
+
+ /// <summary>
+ /// Serialize object to encrypted string.
+ /// </summary>
+ /// <typeparam name="T">Type of specified object.</typeparam>
+ /// <param name="obj">The specified object.</param>
+ public String SerializeToEncryptedString<T>(T obj)
+ {
+ var plainText = SerializeToString(obj);
+ var cypherText = CryptographyHelper.Encrypt(plainText);
+ return cypherText;
+ }
+
+ /// <summary>
+ /// Deserialize object from encrypted file.
+ /// </summary>
+ /// <typeparam name="T">Type of object to deserialize.</typeparam>
+ /// <param name="filePath">The full path of the data file.</param>
+ /// <returns>The resulting object.</returns>
+ public T DeserializeFromEncryptedFile<T>(string filePath)
+ {
+ var cypherText = File.ReadAllText(filePath);
+ var plainText = CryptographyHelper.Decrypt(cypherText);
+ return DeserializeFromString<T>(plainText);
+ }
+
+ /// <summary>
+ /// Deserialize object from encrypted string.
+ /// </summary>
+ /// <typeparam name="T">Type of object to deserialize.</typeparam>
+ /// <returns>The resulting object.</returns>
+ public T DeserializeFromEncryptedString<T>(string encyptedString)
+ {
+ var plainText = CryptographyHelper.Decrypt(encyptedString);
+ return DeserializeFromString<T>(plainText);
+ }
+
+ /// <summary>
+ /// Deserialize object from a file.
+ /// </summary>
+ /// <typeparam name="T">Type of object to deserialize.</typeparam>
+ /// <param name="filePath">The full path of the data file.</param>
+ /// <returns>The resulting object.</returns>
+ public T DeserializeFromFile<T>(string filePath)
+ {
+ using (FileStream fs = new FileStream(filePath, FileMode.Open))
+ {
+ return DeserializeFromStream<T>(fs);
+ }
+ }
+
+
+ /// <summary>
+ /// Serialize object to stream.
+ /// </summary>
+ /// <typeparam name="T">Type of specified object.</typeparam>
+ /// <param name="obj">The specified object.</param>
+ /// <param name="st">The stream to write.</param>
+ public void SerializeToStream<T>(T obj, Stream st)
+ {
+ XmlSerializer f = XmlSerializer.FromTypes(new[] { typeof(T) })[0]; //Microsoft bug workaround.
+ f.Serialize(st, obj);
+ }
+
+ /// <summary>
+ /// Serialize object to string.
+ /// </summary>
+ /// <typeparam name="T">Type of specified object.</typeparam>
+ /// <param name="obj">The specified object.</param>
+ /// <returns></returns>
+ public String SerializeToString<T>(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();
+ }
+ }
+
+ /// <summary>
+ /// Deserialize object from stream.
+ /// </summary>
+ /// <typeparam name="T">Type of object to deserialize.</typeparam>
+ /// <param name="st">Stream to read from.</param>
+ /// <returns>The resulting object.</returns>
+ public T DeserializeFromStream<T>(Stream st)
+ {
+ XmlSerializer f = XmlSerializer.FromTypes(new[] { typeof(T) })[0]; //Microsoft bug workaround.
+ return (T)f.Deserialize(st);
+ }
+
+ /// <summary>
+ /// Deserialize object from string.
+ /// </summary>
+ /// <typeparam name="T">Type of specified object.</typeparam>
+ /// <param name="obj">The specified object.</param>
+ /// <returns></returns>
+ public T DeserializeFromString<T>(String str)
+ {
+ XmlSerializer f = XmlSerializer.FromTypes(new[] { typeof(T) })[0]; //Microsoft bug workaround.
+ using (TextReader reader = new StringReader(str))
+ {
+ return (T)f.Deserialize(reader);
+ }
+ }
+
+ /// <summary>
+ /// Returns the serializer full name.
+ /// </summary>
+ /// <returns></returns>
+ public override string ToString()
+ {
+ return SerializationHelper.GetSerializerName(this);
+ }
+
+ }
+}
diff --git a/Software/Visual_Studio/Tango.Serialization/packages.config b/Software/Visual_Studio/Tango.Serialization/packages.config
new file mode 100644
index 000000000..dbb73d29f
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Serialization/packages.config
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+ <package id="Google.Protobuf" version="3.5.0" targetFramework="net46" />
+ <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net46" />
+</packages> \ No newline at end of file