andrew4582

SerializationHelper

Aug 4th, 2010
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.44 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.Xml;
  6. using System.Xml.Serialization;
  7. using System.Text;
  8. using System.Web.UI;
  9.  
  10. namespace Core.IO {
  11.     /// <summary>
  12.     /// Utility class used for serializing objects
  13.     /// </summary>
  14.     public class SerializationHelper {
  15.         /// <summary>
  16.         /// Deserializes an object from a Base64String string
  17.         /// </summary>
  18.         /// <param name="s">Base64String string</param>
  19.         /// <returns>Returns the object that was serialize using the SaveObjectToString() method</returns>
  20.         public static object ReadObjectFromString(string s) {
  21.             using(MemoryStream memory = new MemoryStream(Convert.FromBase64String(s))) {
  22.                 return ReadObjectFromStream(memory);
  23.             }
  24.         }
  25.         /// <summary>
  26.         /// Uses DataContractSerializer
  27.         /// </summary>
  28.         /// <typeparam name="T"></typeparam>
  29.         /// <param name="source"></param>
  30.         /// <returns></returns>
  31.         public static T ReadObjectFromBytes<T>(byte[] source) {
  32.             Type objectType = typeof(T);
  33.             return (T)ReadObjectFromBytes(source,objectType);
  34.         }
  35.         /// <summary>
  36.         /// DataContractSerializer
  37.         /// </summary>
  38.         /// <param name="source"></param>
  39.         /// <param name="objectType"></param>
  40.         /// <returns></returns>
  41.         public static object ReadObjectFromBytes(byte[] source,Type objectType) {
  42.             var dcs = new System.Runtime.Serialization.DataContractSerializer(objectType);
  43.             using(var memory = new System.IO.MemoryStream(source)) {
  44.                 memory.Seek(0,System.IO.SeekOrigin.Begin);
  45.                 return dcs.ReadObject(memory);
  46.             }
  47.         }
  48.         /// <summary>
  49.         /// Deserializes an object from a stream
  50.         /// </summary>
  51.         /// <param name="s">stream that the object was serialized into</param>
  52.         /// <returns>Returns the object that was serialize using the SaveObjectToStream() method</returns>
  53.         public static object ReadObjectFromStream(Stream s) {
  54.             s.Position = 0;
  55.             return new BinaryFormatter().Deserialize(s);
  56.         }
  57.         public static object ReadObjectFromStreamLos(Stream s) {
  58.             s.Position = 0;
  59.             return new LosFormatter().Deserialize(s);
  60.         }
  61.         /// <summary>
  62.         /// Serializes an object into a memory stream
  63.         /// </summary>
  64.         /// <param name="obj"></param>
  65.         /// <returns></returns>
  66.         public static MemoryStream SaveObjectToStream(object obj) {
  67.             MemoryStream memory = new MemoryStream();
  68.             new BinaryFormatter().Serialize(memory,obj);
  69.             return memory;
  70.         }
  71.         public static MemoryStream SaveObjectToStreamLos(object obj) {
  72.             MemoryStream memory = new MemoryStream();
  73.             new LosFormatter().Serialize(memory,obj);
  74.             return memory;
  75.         }
  76.         public static byte[] SaveObjectToBytes(object source) {
  77.             using(MemoryStream memory = new MemoryStream()) {
  78.                 new BinaryFormatter().Serialize(memory,source);
  79.                 memory.Position = 0;
  80.                 return memory.ToArray();
  81.             }
  82.         }
  83.         /// <summary>
  84.         /// Uses DataContractSerializer
  85.         /// </summary>
  86.         /// <param name="source"></param>
  87.         /// <returns></returns>
  88.         public static byte[] WriteObjectToBytes(object source) {
  89.             var dcs = new System.Runtime.Serialization.DataContractSerializer(source.GetType());
  90.             using(var memory = new System.IO.MemoryStream()) {
  91.                 dcs.WriteObject(memory,source);
  92.                 memory.Seek(0,System.IO.SeekOrigin.Begin);
  93.                 return memory.ToArray();
  94.             }
  95.         }
  96.         /// <summary>
  97.         /// Serializes an object into a Base64 string
  98.         /// </summary>
  99.         /// <param name="obj"></param>
  100.         /// <returns></returns>
  101.         public static string SaveObjectToString(object obj) {
  102.             using(MemoryStream memory = SaveObjectToStream(obj)) {
  103.                 memory.Position = 0;
  104.                 return Convert.ToBase64String(memory.ToArray());
  105.             }
  106.         }
  107.         /// <summary>
  108.         /// Deserilizes an object from a file
  109.         /// </summary>
  110.         /// <param name="FileName"></param>
  111.         /// <returns></returns>
  112.         public static object ReadObjectFromFile(string FileName) {
  113.             using(Stream stm = File.Open(FileName,FileMode.Open)) {
  114.                 BinaryFormatter bformatter = new BinaryFormatter();
  115.                 stm.Position = 0;
  116.                 return bformatter.Deserialize(stm);
  117.             }
  118.         }
  119.         /// <summary>
  120.         /// Serializes an object into a file
  121.         /// </summary>
  122.         /// <param name="Object"></param>
  123.         /// <param name="FileName"></param>
  124.         /// <returns></returns>
  125.         public static bool SaveObjectToFile(object data,string FileName) {
  126.             using(Stream stm = File.Open(FileName,FileMode.Create)) {
  127.                 BinaryFormatter bformatter = new BinaryFormatter();
  128.                 bformatter.Serialize(stm,data);
  129.                 return true;
  130.             }
  131.         }
  132.         /// <summary>
  133.         /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
  134.         /// </summary>
  135.         /// <param name="characters">Unicode Byte Array to be converted to String</param>
  136.         /// <returns>String converted from Unicode Byte Array</returns>
  137.         private static String UTF8ByteArrayToString(byte[] characters) {
  138.             UTF8Encoding encoding = new UTF8Encoding();
  139.             string constructedString = encoding.GetString(characters);
  140.             return (constructedString);
  141.         }
  142.         /// <summary>
  143.         /// Converts the String to UTF8 Byte array and is used in De serialization
  144.         /// </summary>
  145.         /// <param name="pXmlString"></param>
  146.         /// <returns></returns>
  147.         private static Byte[] StringToUTF8ByteArray(string xml) {
  148.             UTF8Encoding encoding = new UTF8Encoding();
  149.             byte[] byteArray = encoding.GetBytes(xml);
  150.             return byteArray;
  151.         }
  152.         public static String SerializeXmlObject(object pObject,Type objectType) {
  153.             using(MemoryStream memoryStream = new MemoryStream()) {
  154.                 XmlSerializer xs = new XmlSerializer(objectType);
  155.                 XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
  156.                 xmlNamespace.Add(string.Empty,string.Empty);
  157.                 xs.Serialize(memoryStream,pObject,xmlNamespace);
  158.                 return UTF8ByteArrayToString(memoryStream.ToArray());
  159.             }
  160.         }
  161.         /// <summary>
  162.         /// Method to reconstruct an Object from XML string
  163.         /// </summary>
  164.         /// <param name="pXmlizedString"></param>
  165.         /// <returns></returns>
  166.         public static object DeserializeXmlObject(string xml,Type objectType) {
  167.             XmlSerializer xs = new XmlSerializer(objectType);
  168.             using(MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(xml))) {
  169.                 XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,Encoding.UTF8);
  170.                 return xs.Deserialize(memoryStream);
  171.             }
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment