Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Runtime.Serialization;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Xml;
- using System.Xml.Serialization;
- using System.Text;
- using System.Web.UI;
- namespace Core.IO {
- /// <summary>
- /// Utility class used for serializing objects
- /// </summary>
- public class SerializationHelper {
- /// <summary>
- /// Deserializes an object from a Base64String string
- /// </summary>
- /// <param name="s">Base64String string</param>
- /// <returns>Returns the object that was serialize using the SaveObjectToString() method</returns>
- public static object ReadObjectFromString(string s) {
- using(MemoryStream memory = new MemoryStream(Convert.FromBase64String(s))) {
- return ReadObjectFromStream(memory);
- }
- }
- /// <summary>
- /// Uses DataContractSerializer
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="source"></param>
- /// <returns></returns>
- public static T ReadObjectFromBytes<T>(byte[] source) {
- Type objectType = typeof(T);
- return (T)ReadObjectFromBytes(source,objectType);
- }
- /// <summary>
- /// DataContractSerializer
- /// </summary>
- /// <param name="source"></param>
- /// <param name="objectType"></param>
- /// <returns></returns>
- public static object ReadObjectFromBytes(byte[] source,Type objectType) {
- var dcs = new System.Runtime.Serialization.DataContractSerializer(objectType);
- using(var memory = new System.IO.MemoryStream(source)) {
- memory.Seek(0,System.IO.SeekOrigin.Begin);
- return dcs.ReadObject(memory);
- }
- }
- /// <summary>
- /// Deserializes an object from a stream
- /// </summary>
- /// <param name="s">stream that the object was serialized into</param>
- /// <returns>Returns the object that was serialize using the SaveObjectToStream() method</returns>
- public static object ReadObjectFromStream(Stream s) {
- s.Position = 0;
- return new BinaryFormatter().Deserialize(s);
- }
- public static object ReadObjectFromStreamLos(Stream s) {
- s.Position = 0;
- return new LosFormatter().Deserialize(s);
- }
- /// <summary>
- /// Serializes an object into a memory stream
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static MemoryStream SaveObjectToStream(object obj) {
- MemoryStream memory = new MemoryStream();
- new BinaryFormatter().Serialize(memory,obj);
- return memory;
- }
- public static MemoryStream SaveObjectToStreamLos(object obj) {
- MemoryStream memory = new MemoryStream();
- new LosFormatter().Serialize(memory,obj);
- return memory;
- }
- public static byte[] SaveObjectToBytes(object source) {
- using(MemoryStream memory = new MemoryStream()) {
- new BinaryFormatter().Serialize(memory,source);
- memory.Position = 0;
- return memory.ToArray();
- }
- }
- /// <summary>
- /// Uses DataContractSerializer
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- public static byte[] WriteObjectToBytes(object source) {
- var dcs = new System.Runtime.Serialization.DataContractSerializer(source.GetType());
- using(var memory = new System.IO.MemoryStream()) {
- dcs.WriteObject(memory,source);
- memory.Seek(0,System.IO.SeekOrigin.Begin);
- return memory.ToArray();
- }
- }
- /// <summary>
- /// Serializes an object into a Base64 string
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string SaveObjectToString(object obj) {
- using(MemoryStream memory = SaveObjectToStream(obj)) {
- memory.Position = 0;
- return Convert.ToBase64String(memory.ToArray());
- }
- }
- /// <summary>
- /// Deserilizes an object from a file
- /// </summary>
- /// <param name="FileName"></param>
- /// <returns></returns>
- public static object ReadObjectFromFile(string FileName) {
- using(Stream stm = File.Open(FileName,FileMode.Open)) {
- BinaryFormatter bformatter = new BinaryFormatter();
- stm.Position = 0;
- return bformatter.Deserialize(stm);
- }
- }
- /// <summary>
- /// Serializes an object into a file
- /// </summary>
- /// <param name="Object"></param>
- /// <param name="FileName"></param>
- /// <returns></returns>
- public static bool SaveObjectToFile(object data,string FileName) {
- using(Stream stm = File.Open(FileName,FileMode.Create)) {
- BinaryFormatter bformatter = new BinaryFormatter();
- bformatter.Serialize(stm,data);
- return true;
- }
- }
- /// <summary>
- /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
- /// </summary>
- /// <param name="characters">Unicode Byte Array to be converted to String</param>
- /// <returns>String converted from Unicode Byte Array</returns>
- private static String UTF8ByteArrayToString(byte[] characters) {
- UTF8Encoding encoding = new UTF8Encoding();
- string constructedString = encoding.GetString(characters);
- return (constructedString);
- }
- /// <summary>
- /// Converts the String to UTF8 Byte array and is used in De serialization
- /// </summary>
- /// <param name="pXmlString"></param>
- /// <returns></returns>
- private static Byte[] StringToUTF8ByteArray(string xml) {
- UTF8Encoding encoding = new UTF8Encoding();
- byte[] byteArray = encoding.GetBytes(xml);
- return byteArray;
- }
- public static String SerializeXmlObject(object pObject,Type objectType) {
- using(MemoryStream memoryStream = new MemoryStream()) {
- XmlSerializer xs = new XmlSerializer(objectType);
- XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
- xmlNamespace.Add(string.Empty,string.Empty);
- xs.Serialize(memoryStream,pObject,xmlNamespace);
- return UTF8ByteArrayToString(memoryStream.ToArray());
- }
- }
- /// <summary>
- /// Method to reconstruct an Object from XML string
- /// </summary>
- /// <param name="pXmlizedString"></param>
- /// <returns></returns>
- public static object DeserializeXmlObject(string xml,Type objectType) {
- XmlSerializer xs = new XmlSerializer(objectType);
- using(MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(xml))) {
- XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,Encoding.UTF8);
- return xs.Deserialize(memoryStream);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment