Created
January 30, 2014 14:51
-
-
Save bradjolicoeur/8710147 to your computer and use it in GitHub Desktop.
Serialization Helper
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; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.Diagnostics; | |
using System.Xml.Serialization; | |
using System.Xml; | |
using System.IO; | |
using System.Runtime.Serialization.Json; | |
using System.Text; | |
public static class SerializationHelper | |
{ | |
/// <summary> | |
/// Deserialize the contents of a file that contains well formed xml | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="filePath"></param> | |
/// <returns></returns> | |
public static T DeserializeFile<T>(string filePath) | |
{ | |
return Deserialize<T>(File.ReadAllText(filePath)); | |
} | |
/// <summary> | |
/// Deserialize an XML string into an object | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="xml"></param> | |
/// <returns></returns> | |
/// <remarks></remarks> | |
public static T Deserialize<T>(string xml) | |
{ | |
if (string.IsNullOrEmpty(xml)) | |
{ | |
return default(T); | |
} | |
XmlSerializer serializer = new XmlSerializer(typeof(T)); | |
XmlReaderSettings settings = new XmlReaderSettings(); | |
// No settings need modifying here | |
using (StringReader textReader = new StringReader(xml)) | |
{ | |
using (XmlReader xmlReader = XmlReader.Create(textReader, settings)) | |
{ | |
return (T)serializer.Deserialize(xmlReader); | |
} | |
} | |
} | |
/// <summary> | |
/// Serialize an object into an XML String | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="value"></param> | |
/// <returns></returns> | |
public static string Serialize<T>(T value) | |
{ | |
if (value == null) | |
{ | |
return null; | |
} | |
XmlSerializer serializer = new XmlSerializer(typeof(T)); | |
XmlWriterSettings settings = new XmlWriterSettings(); | |
settings.Encoding = new UnicodeEncoding(false, false); // no BOM in a .NET string | |
settings.Indent = false; | |
settings.OmitXmlDeclaration = false; | |
using (StringWriter textWriter = new StringWriter()) | |
{ | |
using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings)) | |
{ | |
serializer.Serialize(xmlWriter, value); | |
} | |
return textWriter.ToString(); | |
} | |
} | |
public static string JSONSerialize(object value) | |
{ | |
string response = ""; | |
MemoryStream stream = new MemoryStream(); | |
DataContractJsonSerializer ser = new DataContractJsonSerializer(value.GetType()); | |
ser.WriteObject(stream, value); | |
response = Encoding.Default.GetString(stream.ToArray()); | |
return response; | |
} | |
public static T JSONDeserialize<T>(string json) | |
{ | |
using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json))) | |
{ | |
var serializer = new DataContractJsonSerializer(typeof(T)); | |
return (T)serializer.ReadObject(ms); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment