-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerializationUtility.cs
100 lines (89 loc) · 3.62 KB
/
SerializationUtility.cs
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
/*********************************************************************************
This file is part of Imagizer2.
Imagizer2 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Imagizer2 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Imagizer2. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
namespace Imagizer2
{
public static class SerializationUtility
{
public static T DeserializeObject<T>(String pXmlizedString)
{
return (T)DeserializeObject(pXmlizedString, typeof(T));
}
/// <summary>
/// Method to reconstruct an Object from XML string
/// </summary>
/// <param name="pXmlizedString"></param>
/// <returns></returns>
public static Object DeserializeObject(String pXmlizedString, Type thisType)
{
try
{
StringReader reader = new StringReader(pXmlizedString);
XmlSerializer xs2 = new XmlSerializer(thisType);
return xs2.Deserialize(reader);
}
catch (Exception e)
{
System.Console.WriteLine(e);
throw;
}
}
/// <summary>
/// Method to convert a custom Object to XML string
/// </summary>
/// <param name="pObject">Object that is to be serialized to XML</param>
/// <returns>XML string</returns>
public static String SerializeObject(Object objToSerialize)
{
try
{
Type thisType = objToSerialize.GetType();
StringWriter writer = new StringWriter();
XmlSerializer xs = new XmlSerializer(thisType);
xs.Serialize(writer, objToSerialize);
return writer.ToString();
}
catch (Exception e)
{
System.Console.WriteLine(e);
throw;
}
}
/// <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>
public static String UTF8ByteArrayToString(Byte[] characters)
{
UTF8Encoding encoding = new UTF8Encoding();
String constructedString = encoding.GetString(characters, 0, characters.Length);
return (constructedString);
}
/// <summary>
/// Converts the String to UTF8 Byte array and is used in De serialization
/// </summary>
/// <param name="pXmlString"></param>
/// <returns></returns>
public static Byte[] StringToUTF8ByteArray(String pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(pXmlString);
return byteArray;
}
}
}