Serialization C#

Various serialization functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
}

1
2
3
4
5
6
7
8
9
10
11
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);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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;";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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