Serialization C#

Various serialization functions

public static string SerializeObject(object obj)
{
    if (obj == null) { return string.Empty; }

    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
    string s = string.Empty;

    using (System.IO.StringWriter sw = new System.IO.StringWriter())
    {
        serializer.Serialize(sw, obj);
        s = sw.ToString();
    }

    return s;
}

public static T DeserializeObject<T>(string xml)
{
    if (string.IsNullOrEmpty(xml)) { return default(T); }

    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));

    using (System.IO.StringReader reader = new System.IO.StringReader(xml))
    {
        return (T)serializer.Deserialize(reader);
    }
}
public static string SerializeObject(object obj)
{
    if (obj == null)
    {
        return "nul;";
    }
    else if (obj is string)
    {
        string s = (string)obj;
        return "str:" + s.Length + ":" + s + ";";
    }
    else if (obj is bool)
    {
        bool b = (bool)obj;
        return "boo:" + b.ToString() + ";";
    }
    else if (obj is int)
    {
        int i = (int)obj;
        return "int:" + i.ToString() + ";";
    }
    else if (obj is double)
    {
        double d = (double)obj;
        return "dou:" + d.ToString() + ";";
    }
    else if (obj is decimal)
    {
        decimal d = (decimal)obj;
        return "dec:" + d.ToString() + ";";
    }
    else if (obj is DateTime)
    {
        DateTime d = (DateTime)obj;
        string year = d.Year.ToString();
        string month = d.Month.ToString();
        string day = d.Day.ToString();
        string hour = d.Hour.ToString();
        string min = d.Minute.ToString();
        string sec = d.Second.ToString();

        return "dat:" + year + ":" + month + ":" + day + ":" + hour + ":" + min + ":" + sec + ";";
    }
    else
    {
        // un-handled data type: set to null
        return "nul;";
    }
}
public static object DeserializeObject(string str)
{
    if (string.IsNullOrEmpty(str))
    {
        return new Object();
    }

    if (str.StartsWith("nul;"))
    {
        return null;
    }
    else if (str.StartsWith("boo:"))
    {
        #region boolean
        // booleans formatted like this:
        // boo:True;
        // boo:False;

        int start_pos = str.IndexOf(":") + 1;
        int end_pos = str.IndexOf(";");

        if (str.Substring(start_pos, end_pos - start_pos).ToUpper() == "FALSE")
        {
            bool b = false;
            return b;
        }
        else
        {
            bool b = true;
            return b;
        }
        #endregion
    }
    else if (str.StartsWith("int:"))
    {
        #region integer
        // integers formatted like this:
        // int:12345;
        // int:-12345;

        int start_pos = str.IndexOf(":") + 1;
        int end_pos = str.IndexOf(";");

        int i = Int32.Parse(str.Substring(start_pos, end_pos - start_pos));
        return i;
        #endregion
    }
    else if (str.StartsWith("dec:"))
    {
        #region decimal
        // decimals formatted like this:
        // dec:12345.67;
        // dec:-12345.67;

        int start_pos = str.IndexOf(":") + 1;
        int end_pos = str.IndexOf(";");

        decimal d = Decimal.Parse(str.Substring(start_pos, end_pos - start_pos));
        return d;
        #endregion
    }
    else if (str.StartsWith("dou:"))
    {
        #region double
        // doubles formatted like this:
        // dou:12345.67;
        // dou:-12345.67;

        int start_pos = str.IndexOf(":") + 1;
        int end_pos = str.IndexOf(";");

        double d = Double.Parse(str.Substring(start_pos, end_pos - start_pos));
        return d;
        #endregion
    }
    else if (str.StartsWith("str:"))
    {
        #region string
        // strings are formatted like this:
        // str:11:hello world;
        // str:0:;
        //
        string[] arry = str.Split(':');
        string s = arry[2];
        if (!string.IsNullOrEmpty(s))
        {
            return s.Substring(0, s.Length - 1);
        }
        else
        {
            return string.Empty;
        }
        #endregion
    }
    else if (str.StartsWith("dat:"))
    {
        #region date
        // dates are formatted like this:
        // dat:2012:12:31:23:59:59;
        //
        str = str.Substring(0, str.Length - 1); // trim off the trailing ;
        string[] arry = str.Split(':');
        DateTime d = new DateTime(
            Int32.Parse(arry[1]),
            Int32.Parse(arry[2]),
            Int32.Parse(arry[3]),
            Int32.Parse(arry[4]),
            Int32.Parse(arry[5]),
            Int32.Parse(arry[6]));
        return d;
        #endregion
    }
    else
    {
        throw new NotSupportedException("Deserialize failed: unknown data type serialized");
    }
}

Leave a Reply