From 04a5cc87f87f35c7a297c321396b89ecbb83b8d3 Mon Sep 17 00:00:00 2001 From: Gabriel Monteagudo Date: Sun, 6 Jul 2014 16:33:51 -0300 Subject: [PATCH] Improved encoding handling. --- .gitattributes | 3 - NetworkHelper/NetworkHelperConfiguration.xml | Bin 1392 -> 676 bytes .../Utilities/ConfigurationManager.cs | 4 +- NetworkHelper/Utilities/Serializer.cs | 237 ++---------------- 4 files changed, 24 insertions(+), 220 deletions(-) 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 104ac9ad07a304efbd109c9f8431565530cf00a8..ebd901d06d8f272af35ab36ba20762ebe89cc330 100755 GIT binary patch literal 676 zcmbVK%WlFj5WMphmhadJkBTB=gvzB-K~<3uS2uBjMPo-^r=j20LqDm%P{$BTsVbx% zB28v@c4v3}`ToJC`{D(?N(;*9364pGk<^JvmCh%)a#?qTr@X`NjLuYES>b#h@)`}} zJ*WxZI#-S zh2)Djw0>M7?J=W)35h;D*RZOxOzkC;Al zQ=GuZjozQqbA}Bgj_7A%IvG|>lq-GLm+vO=6GoWP+8~n}u{=DY zhdT_&XHH%b<5!hc$`~W%IZ@6gFN+l(1*?+f=};9PL$VeGle^fq?M16O>62qb-T|}t zp!X2>=yQ)VyUz@*k0_c)HTS!UH=wdpt_4*Lh@WUomtWTIYQ-Y5^sw(%IranNNL44L zjB*Y>tWP;R>($j5W7cj#h4T6b=rEg*nM$Re_@wl6^u5aZ&#)B+<=eZAcku*AbjQu)id&$+Vz2JVv<+9V7nNH&OmpJ>l2>0LgIfjBVQ!<63UBWJKI4d$I zOXY6!Pq%XVFEv|Kv$lnMsFtSNd)gyQ)~$gSms{Emmi0TNF2%Z)r`gNq+;+U&D9*lE Je*W{Bo8QjT%ftWx 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 +}