Skip to content

Commit

Permalink
Improved encoding handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielm-infocorp committed Jul 6, 2014
1 parent 0230e11 commit 04a5cc8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 220 deletions.
3 changes: 0 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified NetworkHelper/NetworkHelperConfiguration.xml
Binary file not shown.
4 changes: 2 additions & 2 deletions NetworkHelper/Utilities/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ public static void LoadConfiguration()
}
else
{
Configuration = Serializer.XmlDeserialize<Configuration>(File.ReadAllText(ConfigurationFilePath.Value, Encoding.BigEndianUnicode));
Configuration = Serializer.XmlDeserialize<Configuration>(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()
Expand Down
237 changes: 22 additions & 215 deletions NetworkHelper/Utilities/Serializer.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Helper class to serialize and deserialize in various ways
/// </summary>
public static class Serializer
{
#region XmlSerialization

#region Non-generic methods

#region Serialization methods

/// <summary>
/// Serialize from object to string using XmlSerializer
/// </summary>
/// <param name="type">Type of object</param>
/// <param name="serializableObject">Instance of type</param>
/// <param name="includeXmlDeclaration">Specifies whether to include the XML declaration in the serialization</param>
/// <param name="knownTypes">Types that may be present in the object graph</param>
/// <returns>A string with the serialized object</returns>
public static string XmlSerialize(Type type, object serializableObject, bool includeXmlDeclaration, params Type[] knownTypes)
public static string XmlSerialize<T>(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<T>(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;
}

/// <summary>
/// Serialize from object to string using XmlSerializer
/// </summary>
/// <param name="type">Type of object</param>
/// <param name="serializableObject">Instance of type</param>
/// <param name="includeXmlDeclaration">Specifies whether to include the XML declaration in the serialization</param>
/// <returns>A string with the serialized object</returns>
public static string XmlSerialize(Type type, object serializableObject, bool includeXmlDeclaration)
{
return XmlSerialize(type, serializableObject, includeXmlDeclaration, null);
}

/// <summary>
/// Serialize from object to string using XmlSerializer, including the XML declaration
/// </summary>
/// <param name="type">Type of object</param>
/// <param name="serializableObject">Instance of type</param>
/// <param name="knownTypes">Types that may be present in the object graph</param>
/// <returns>A string with the serialized object</returns>
public static string XmlSerialize(Type type, object serializableObject, params Type[] knownTypes)
{
return XmlSerialize(type, serializableObject, true, knownTypes);
}

/// <summary>
/// Serialize from object to string using XmlSerializer, including the XML declaration
/// </summary>
/// <param name="type">Type of object</param>
/// <param name="serializableObject">Instance of type</param>
/// <returns>A string with the serialized object</returns>
public static string XmlSerialize(Type type, object serializableObject)
{
return XmlSerialize(type, serializableObject, true, null);
}

#endregion

#region Deserialization methods

/// <summary>
/// Deserialize from string to object using XmlSerializer
/// </summary>
/// <param name="type">Type of serialized object</param>
/// <param name="serializedObject">Instance of serialized object</param>
/// <param name="knownTypes">Types that may be present in the object graph</param>
/// <returns>An instance of object type</returns>
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;
}

/// <summary>
/// Deserialize from string to object using XmlSerializer
/// </summary>
/// <param name="type">Type of serialized object</param>
/// <param name="serializedObject">Instance of serialized object</param>
/// <returns>An instance of object type</returns>
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

/// <summary>
/// Serialize from object to string using XmlSerializer
/// </summary>
/// <typeparam name="T">Type of object</typeparam>
/// <param name="serializableObject">Instance of T</param>
/// <param name="includeXmlDeclaration">Specifies whether to include the XML declaration in the serialization</param>
/// <param name="knownTypes">Types that may be present in the object graph</param>
/// <returns>A string with the serialized object</returns>
public static string XmlSerialize<T>(T serializableObject, bool includeXmlDeclaration, params Type[] knownTypes)
{
return XmlSerialize(typeof(T), serializableObject, includeXmlDeclaration, knownTypes);
}

/// <summary>
/// Serialize from object to string using XmlSerializer, including the XML declaration
/// </summary>
/// <typeparam name="T">Type of object</typeparam>
/// <param name="serializableObject">Instance of T</param>
/// <param name="knownTypes">Types that may be present in the object graph</param>
/// <returns>A string with the serialized object</returns>
public static string XmlSerialize<T>(T serializableObject, params Type[] knownTypes)
{
return XmlSerialize(typeof(T), serializableObject, true, knownTypes);
}

/// <summary>
/// Serialize from object to string using XmlSerializer
/// </summary>
/// <typeparam name="T">Type of object</typeparam>
/// <param name="serializableObject">Instance of T</param>
/// <param name="includeXmlDeclaration">Specifies whether to include the XML declaration in the serialization</param>
/// <returns>A string with the serialized object</returns>
public static string XmlSerialize<T>(T serializableObject, bool includeXmlDeclaration)
{
return XmlSerialize(typeof(T), serializableObject, includeXmlDeclaration, null);
}

/// <summary>
/// Serialize from object to string using XmlSerializer, including the XML declaration
/// </summary>
/// <typeparam name="T">Type of object</typeparam>
/// <param name="serializableObject">Instance of T</param>
/// <returns>A string with the serialized object</returns>
public static string XmlSerialize<T>(T serializableObject)
{
return XmlSerialize(typeof(T), serializableObject, true, null);
}

#endregion

#region Deserialization methods

/// <summary>
/// Deserialize from string to object using XmlSerializer
/// </summary>
/// <typeparam name="T">Type of serialized object</typeparam>
/// <param name="serializedObject">Instance of serialized object</param>
/// <param name="knownTypes">Types that may be present in the object graph</param>
/// <returns>An instance of object T</returns>
public static T XmlDeserialize<T>(string serializedObject, params Type[] knownTypes)
{
return (T)XmlDeserialize(typeof(T), serializedObject, knownTypes);
}

/// <summary>
/// Deserialize from string to object using XmlSerializer
/// </summary>
/// <typeparam name="T">Type of serialized object</typeparam>
/// <param name="serializedObject">Instance of serialized object</param>
/// <returns>An instance of object T</returns>
public static T XmlDeserialize<T>(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
}
}
}

0 comments on commit 04a5cc8

Please sign in to comment.