diff --git a/.gitattributes b/.gitattributes index 9ba4cca..1bc5f9f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -47,9 +47,6 @@ *.dll binary *.pdb binary -# Compare .pbxproj files as binary and always merge as union -*.pbxproj binary -merge=union - # Custom for Visual Studio *.sln merge=union *.csproj merge=union diff --git a/NetworkHelper/NetworkHelperConfiguration.xml b/NetworkHelper/NetworkHelperConfiguration.xml index 104ac9a..ebd901d 100755 Binary files a/NetworkHelper/NetworkHelperConfiguration.xml and b/NetworkHelper/NetworkHelperConfiguration.xml differ diff --git a/NetworkHelper/Utilities/ConfigurationManager.cs b/NetworkHelper/Utilities/ConfigurationManager.cs index f539391..edfb37c 100755 --- a/NetworkHelper/Utilities/ConfigurationManager.cs +++ b/NetworkHelper/Utilities/ConfigurationManager.cs @@ -41,14 +41,14 @@ public static void LoadConfiguration() } else { - Configuration = Serializer.XmlDeserialize(File.ReadAllText(ConfigurationFilePath.Value, Encoding.BigEndianUnicode)); + Configuration = Serializer.XmlDeserialize(File.ReadAllText(ConfigurationFilePath.Value, Encoding.UTF8)); InitializeAndValidateConfigurationAndCreateExampleIfNeeded(); } } public static void SaveConfiguration() { - File.WriteAllText(ConfigurationFilePath.Value, Serializer.XmlSerialize(Configuration), Encoding.BigEndianUnicode); + File.WriteAllText(ConfigurationFilePath.Value, Serializer.XmlSerialize(Configuration), Encoding.UTF8); } private static void InitializeAndValidateConfigurationAndCreateExampleIfNeeded() diff --git a/NetworkHelper/Utilities/Serializer.cs b/NetworkHelper/Utilities/Serializer.cs index b8c2576..42d277f 100755 --- a/NetworkHelper/Utilities/Serializer.cs +++ b/NetworkHelper/Utilities/Serializer.cs @@ -1,252 +1,59 @@ using System; -using System.Globalization; using System.IO; -using System.Linq; -using System.Xml; +using System.Text; using System.Xml.Serialization; namespace NetworkHelper.Utilities { - /// - /// Helper class to serialize and deserialize in various ways - /// public static class Serializer { - #region XmlSerialization - - #region Non-generic methods - - #region Serialization methods - - /// - /// Serialize from object to string using XmlSerializer - /// - /// Type of object - /// Instance of type - /// Specifies whether to include the XML declaration in the serialization - /// Types that may be present in the object graph - /// A string with the serialized object - public static string XmlSerialize(Type type, object serializableObject, bool includeXmlDeclaration, params Type[] knownTypes) + public static string XmlSerialize(T serializableObject) { - if (type == null) - { - throw new ArgumentNullException("type"); - } + return XmlSerialize(typeof(T), serializableObject); + } - if (serializableObject == null) - { - throw new ArgumentNullException("serializableObject"); - } + public static T XmlDeserialize(string serializedObject) + { + return (T)XmlDeserialize(typeof(T), serializedObject); + } + private static string XmlSerialize(Type type, object serializableObject) + { string result; - using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture)) + using (StringWriter stringWriter = new Utf8StringWriter()) { - XmlSerializer xmlSerializer = GetXmlSerializer(type, knownTypes); + XmlSerializer xmlSerializer = new XmlSerializer(type); xmlSerializer.Serialize(stringWriter, serializableObject); - if (includeXmlDeclaration) - { - result = stringWriter.ToString(); - } - else - { - XmlDocument xmlDocument = new XmlDocument(); - xmlDocument.LoadXml(stringWriter.ToString()); - result = xmlDocument.FirstChild.NextSibling.OuterXml; - } + result = stringWriter.ToString(); } return result; } - /// - /// Serialize from object to string using XmlSerializer - /// - /// Type of object - /// Instance of type - /// Specifies whether to include the XML declaration in the serialization - /// A string with the serialized object - public static string XmlSerialize(Type type, object serializableObject, bool includeXmlDeclaration) - { - return XmlSerialize(type, serializableObject, includeXmlDeclaration, null); - } - - /// - /// Serialize from object to string using XmlSerializer, including the XML declaration - /// - /// Type of object - /// Instance of type - /// Types that may be present in the object graph - /// A string with the serialized object - public static string XmlSerialize(Type type, object serializableObject, params Type[] knownTypes) - { - return XmlSerialize(type, serializableObject, true, knownTypes); - } - - /// - /// Serialize from object to string using XmlSerializer, including the XML declaration - /// - /// Type of object - /// Instance of type - /// A string with the serialized object - public static string XmlSerialize(Type type, object serializableObject) - { - return XmlSerialize(type, serializableObject, true, null); - } - - #endregion - - #region Deserialization methods - - /// - /// Deserialize from string to object using XmlSerializer - /// - /// Type of serialized object - /// Instance of serialized object - /// Types that may be present in the object graph - /// An instance of object type - public static object XmlDeserialize(Type type, string serializedObject, params Type[] knownTypes) + private static object XmlDeserialize(Type type, string serializedObject) { - if (type == null) - { - throw new ArgumentNullException("type"); - } - - if (serializedObject == null) - { - throw new ArgumentNullException("serializedObject"); - } - object result; using (StringReader stringReader = new StringReader(serializedObject)) { - XmlSerializer xmlSerializer = GetXmlSerializer(type, knownTypes); + XmlSerializer xmlSerializer = new XmlSerializer(type); result = xmlSerializer.Deserialize(stringReader); } return result; } - /// - /// Deserialize from string to object using XmlSerializer - /// - /// Type of serialized object - /// Instance of serialized object - /// An instance of object type - public static object XmlDeserialize(Type type, string serializedObject) + private class Utf8StringWriter : StringWriter { - return XmlDeserialize(type, serializedObject, null); - } - - #endregion - - #endregion - - #region Generic methods - - #region Serialization methods - - /// - /// Serialize from object to string using XmlSerializer - /// - /// Type of object - /// Instance of T - /// Specifies whether to include the XML declaration in the serialization - /// Types that may be present in the object graph - /// A string with the serialized object - public static string XmlSerialize(T serializableObject, bool includeXmlDeclaration, params Type[] knownTypes) - { - return XmlSerialize(typeof(T), serializableObject, includeXmlDeclaration, knownTypes); - } - - /// - /// Serialize from object to string using XmlSerializer, including the XML declaration - /// - /// Type of object - /// Instance of T - /// Types that may be present in the object graph - /// A string with the serialized object - public static string XmlSerialize(T serializableObject, params Type[] knownTypes) - { - return XmlSerialize(typeof(T), serializableObject, true, knownTypes); - } - - /// - /// Serialize from object to string using XmlSerializer - /// - /// Type of object - /// Instance of T - /// Specifies whether to include the XML declaration in the serialization - /// A string with the serialized object - public static string XmlSerialize(T serializableObject, bool includeXmlDeclaration) - { - return XmlSerialize(typeof(T), serializableObject, includeXmlDeclaration, null); - } - - /// - /// Serialize from object to string using XmlSerializer, including the XML declaration - /// - /// Type of object - /// Instance of T - /// A string with the serialized object - public static string XmlSerialize(T serializableObject) - { - return XmlSerialize(typeof(T), serializableObject, true, null); - } - - #endregion - - #region Deserialization methods - - /// - /// Deserialize from string to object using XmlSerializer - /// - /// Type of serialized object - /// Instance of serialized object - /// Types that may be present in the object graph - /// An instance of object T - public static T XmlDeserialize(string serializedObject, params Type[] knownTypes) - { - return (T)XmlDeserialize(typeof(T), serializedObject, knownTypes); - } - - /// - /// Deserialize from string to object using XmlSerializer - /// - /// Type of serialized object - /// Instance of serialized object - /// An instance of object T - public static T XmlDeserialize(string serializedObject) - { - return (T)XmlDeserialize(typeof(T), serializedObject, null); - } - - #endregion - - #endregion - - #endregion - - #region Private members - - private static XmlSerializer GetXmlSerializer(Type type, Type[] knownTypes) - { - XmlSerializer result; - - if (knownTypes != null && knownTypes.Any()) + public override Encoding Encoding { - result = new XmlSerializer(type, knownTypes); - } - else - { - result = new XmlSerializer(type); + get + { + return Encoding.UTF8; + } } - - return result; } - - #endregion } -} \ No newline at end of file +}