Last active
July 29, 2018 19:53
-
-
Save slmcmahon/cf43affcc2fff2893c856b4f594cd495 to your computer and use it in GitHub Desktop.
Helper for XML and JSON Serialization / Deserialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.IO; | |
using System.Text.RegularExpressions; | |
using System.Xml.Serialization; | |
using System.Xml; | |
using System.Runtime.Serialization; | |
namespace Utils | |
{ | |
/// <summary> | |
/// Convenience class for serializing arbitrary, serializable objects. | |
/// </summary> | |
/// <typeparam name="T">The Type of the object to be serialized</typeparam> | |
public static class SerializationUtils<T> | |
{ | |
/// <summary> | |
/// Serializes an object to JSON | |
/// </summary> | |
/// <param name="objToSerialize"></param> | |
/// <returns></returns> | |
public static string SerializeToJSON(T objToSerialize) | |
{ | |
return Newtonsoft.Json.JsonConvert.SerializeObject(objToSerialize, Newtonsoft.Json.Formatting.None); | |
} | |
/// <summary> | |
/// Deserializes a json string to an object | |
/// </summary> | |
/// <param name="JSONString"></param> | |
/// <returns></returns> | |
public static T DeserializeJSONString(string JSONString) | |
{ | |
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(JSONString); | |
} | |
/// <summary> | |
/// Serializes an object to an XmlDocument and returns the FirstChild | |
/// </summary> | |
/// <param name="objToSerialize">The serializable object that will be serialized</param> | |
/// <returns>Xml node (FirstChild)</returns> | |
public static XmlNode SerializeToXmlNode(T objToSerialize) | |
{ | |
string xml = SerializeToString(objToSerialize); | |
XmlDocument xmlDocument = new XmlDocument(); | |
xmlDocument.LoadXml(xml); | |
return xmlDocument.FirstChild; | |
} | |
/// <summary> | |
/// Serializes the given object to XML and removes the encoding string | |
/// </summary> | |
/// <param name="objToSerialize">The serializable object that will be serialized</param> | |
/// <returns>An XML representation of the provided object</returns> | |
public static string SerializeToString(T objToSerialize) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
Serialize(objToSerialize, new StringWriter(sb)); | |
// StringBuilder automatically adds utf-16 as the encoding, so this line | |
// will strip that out so that it doesn't confuse SQL Server | |
var xmlText = Regex.Replace(sb.ToString(), @"\sencoding="".*""", ""); | |
return xmlText; | |
} | |
/// <summary> | |
/// Serializes the given object as XML to the provided path | |
/// </summary> | |
/// <param name="objToSerialize">The object to serialize</param> | |
/// <param name="fileName">path to to save the serialized data to</param> | |
public static void SerializeToFile(T objToSerialize, string fileName) | |
{ | |
Serialize(objToSerialize, new StreamWriter(fileName)); | |
} | |
/// <summary> | |
/// Deserializes the given xml back into an instance of type T | |
/// </summary> | |
/// <param name="xml">XML serialized object</param> | |
/// <param name="type">Type to create</param> | |
/// <returns>An instance of type T</returns> | |
public static T DeserializeString(string xml) | |
{ | |
XmlSerializer ser = new XmlSerializer(typeof(T)); | |
using (XmlTextReader xtr = new XmlTextReader(new StringReader(xml))) | |
{ | |
return (T)ser.Deserialize(xtr); | |
} | |
} | |
/// <summary> | |
/// Deserializes the XML document found at the given path back | |
/// into an instance of type T | |
/// </summary> | |
/// <param name="path">Path to XML document</param> | |
/// <param name="type">Type to create</param> | |
/// <returns>An instance of type T</returns> | |
public static T DeserializePath(String path) | |
{ | |
XmlSerializer ser = new XmlSerializer(typeof(T)); | |
using (StreamReader sr = new StreamReader(path)) | |
{ | |
return (T)ser.Deserialize(sr); | |
} | |
} | |
private static void Serialize(T objToSerialize, TextWriter sw) | |
{ | |
try | |
{ | |
XmlSerializer ser = new XmlSerializer(typeof(T)); | |
using (sw) | |
{ | |
ser.Serialize(sw, objToSerialize); | |
} | |
} | |
catch (Exception ex) | |
{ | |
try | |
{ | |
using (MemoryStream stream = new MemoryStream()) | |
{ | |
DataContractSerializer ser1 = new DataContractSerializer(typeof(T)); | |
ser1.WriteObject(stream, objToSerialize); | |
sw.Write(Encoding.UTF8.GetString(stream.ToArray())); | |
} | |
} | |
catch (Exception) | |
{ | |
sw.WriteLine(ex.Message); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment