forked from siteserver/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializer.cs
More file actions
380 lines (327 loc) · 11.2 KB
/
Serializer.cs
File metadata and controls
380 lines (327 loc) · 11.2 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace SiteServer.Utils
{
public class Serializer
{
//Do not allow this class to be instantiated
private Serializer()
{
}
/// <summary>
/// Static Constructor is used to set the CanBinarySerialize value only once for the given security policy
/// </summary>
static Serializer()
{
var sp = new SecurityPermission(SecurityPermissionFlag.SerializationFormatter);
try
{
sp.Demand();
CanBinarySerialize = true;
}
catch(SecurityException)
{
CanBinarySerialize = false;
}
}
/// <summary>
/// Readonly value indicating if Binary Serialization (using BinaryFormatter) is allowed
/// </summary>
public static readonly bool CanBinarySerialize;
/// <summary>
/// Converts a .NET object to a byte array. Before the conversion happens, a check with
/// Serializer.CanBinarySerialize will be made
/// </summary>
/// <param name="objectToConvert">Object to convert</param>
/// <returns>A byte arry representing the object paramter. Null will be return if CanBinarySerialize is false</returns>
public static byte[] ConvertToBytes(object objectToConvert)
{
byte[] byteArray = null;
if(CanBinarySerialize)
{
var binaryFormatter = new BinaryFormatter();
using(var ms = new MemoryStream())
{
binaryFormatter.Serialize(ms, objectToConvert);
// Set the position of the MemoryStream back to 0
//
ms.Position = 0;
// Read in the byte array
//
byteArray = new Byte[ms.Length];
ms.Read(byteArray, 0, byteArray.Length);
ms.Close();
}
}
return byteArray;
}
/// <summary>
/// Saves an object to disk as a binary file.
/// </summary>
/// <param name="objectToSave">Object to Save</param>
/// <param name="path">Location of the file</param>
/// <returns>true if the save was succesful.</returns>
public static bool SaveAsBinary(object objectToSave, string path)
{
if(objectToSave != null && CanBinarySerialize)
{
var ba = ConvertToBytes(objectToSave);
if(ba != null)
{
using(var fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write))
{
using(var bw = new BinaryWriter(fs))
{
bw.Write(ba);
return true;
}
}
}
}
return false;
}
/// <summary>
/// Converts a .NET object to a string of XML. The object must be marked as Serializable or an exception
/// will be thrown.
/// </summary>
/// <param name="objectToConvert">Object to convert</param>
/// <returns>A xml string represting the object parameter. The return value will be null of the object is null</returns>
public static string ConvertToString(object objectToConvert)
{
string xml = null;
if(objectToConvert != null)
{
//we need the type to serialize
var t = objectToConvert.GetType();
var ser = new XmlSerializer(t);
//will hold the xml
using(var writer = new StringWriter(CultureInfo.InvariantCulture))
{
ser.Serialize(writer, objectToConvert);
xml = writer.ToString();
writer.Close();
}
}
return xml;
}
public static void SaveAsXML(object objectToConvert, string path)
{
if(objectToConvert != null)
{
//we need the type to serialize
var t = objectToConvert.GetType();
var ser = new XmlSerializer(t);
//will hold the xml
using(var writer = new StreamWriter(path))
{
ser.Serialize(writer, objectToConvert);
writer.Close();
}
}
}
/// <summary>
/// Converts a byte array to a .NET object. You will need to cast this object back to its expected type.
/// If the array is null or empty, it will return null.
/// </summary>
/// <param name="byteArray">An array of bytes represeting a .NET object</param>
/// <returns>The byte array converted to an object or null if the value of byteArray is null or empty</returns>
public static object ConvertToObject(byte[] byteArray)
{
object convertedObject = null;
if(CanBinarySerialize && byteArray != null && byteArray.Length > 0)
{
var binaryFormatter = new BinaryFormatter();
using(var ms = new MemoryStream())
{
ms.Write(byteArray, 0, byteArray.Length);
// Set the memory stream position to the beginning of the stream
//
ms.Position = 0;
if( byteArray.Length > 4 )
convertedObject = binaryFormatter.Deserialize(ms);
ms.Close();
}
}
return convertedObject;
}
public static object ConvertFileToObject(string path, Type objectType)
{
object convertedObject = null;
if(path != null && path.Length > 0)
{
using(var fs = new FileStream(path,FileMode.Open,FileAccess.Read))
{
var ser = new XmlSerializer(objectType);
convertedObject = ser.Deserialize(fs);
fs.Close();
}
}
return convertedObject;
}
/// <summary>
/// Converts a string of xml to the supplied object type.
/// </summary>
/// <param name="xml">Xml representing a .NET object</param>
/// <param name="objectType">The type of object which the xml represents</param>
/// <returns>A instance of object or null if the value of xml is null or empty</returns>
public static object ConvertToObject(string xml, Type objectType)
{
object convertedObject = null;
if(!string.IsNullOrEmpty(xml))
{
using(var reader = new StringReader(xml))
{
var ser = new XmlSerializer(objectType);
convertedObject = ser.Deserialize(reader);
reader.Close();
}
}
return convertedObject;
}
/// <summary>
/// Converts a string of xml to the supplied object type.
/// </summary>
/// <param name="xml">Xml representing a .NET object</param>
/// <param name="objectType">The type of object which the xml represents</param>
/// <returns>A instance of object or null if the value of xml is null or empty</returns>
public static object ConvertToObject(XmlNode node, Type objectType)
{
object convertedObject = null;
if(node != null)
{
using(var reader = new StringReader(node.OuterXml))
{
var ser = new XmlSerializer(objectType);
convertedObject = ser.Deserialize(reader);
reader.Close();
}
}
return convertedObject;
}
public static object LoadBinaryFile(string path)
{
if(!File.Exists(path))
return null;
using(var fs = new FileStream(path,FileMode.Open,FileAccess.Read))
{
var br =new BinaryReader(fs);
var ba = new byte[fs.Length];
br.Read(ba,0,(int)fs.Length);
return ConvertToObject(ba);
}
}
/// <summary>
/// Creates a NameValueCollection from two string. The first contains the key pattern and the second contains the values
/// spaced according to the kys
/// </summary>
/// <param name="keys">Keys for the namevalue collection</param>
/// <param name="values">Values for the namevalue collection</param>
/// <returns>A NVC populated based on the keys and vaules</returns>
/// <example>
/// string keys = "key1:S:0:3:key2:S:3:2:";
/// string values = "12345";
/// This would result in a NameValueCollection with two keys (Key1 and Key2) with the values 123 and 45
/// </example>
public static NameValueCollection ConvertToNameValueCollection(string keys, string values)
{
var nvc = new NameValueCollection();
if(keys != null && values != null && keys.Length > 0 && values.Length > 0)
{
var splitter = new char[1] { ':' } ;
var keyNames = keys.Split(splitter);
for (var i = 0; i < (keyNames.Length / 4); i++)
{
var start = int.Parse(keyNames[(i * 4) + 2], CultureInfo.InvariantCulture);
var len = int.Parse(keyNames[(i * 4) + 3], CultureInfo.InvariantCulture);
var key = keyNames[i * 4];
//Future version will support more complex types
if (((keyNames[(i * 4) + 1] == "S") && (start >= 0)) && (len > 0) && (values.Length >= (start + len)))
{
nvc[key] = values.Substring(start, len);
}
}
}
return nvc;
}
public static Dictionary<string, string> ConvertToDictionary(string keys, string values)
{
var nvc = new Dictionary<string, string>();
if (keys != null && values != null && keys.Length > 0 && values.Length > 0)
{
var splitter = new char[1] { ':' };
var keyNames = keys.Split(splitter);
for (var i = 0; i < (keyNames.Length / 4); i++)
{
var start = int.Parse(keyNames[(i * 4) + 2], CultureInfo.InvariantCulture);
var len = int.Parse(keyNames[(i * 4) + 3], CultureInfo.InvariantCulture);
var key = keyNames[i * 4];
//Future version will support more complex types
if (((keyNames[(i * 4) + 1] == "S") && (start >= 0)) && (len > 0) && (values.Length >= (start + len)))
{
nvc[key] = values.Substring(start, len);
}
}
}
return nvc;
}
/// <summary>
/// Creates a the keys and values strings for the simple serialization based on a NameValueCollection
/// </summary>
/// <param name="nvc">NameValueCollection to convert</param>
/// <param name="keys">the ref string will contain the keys based on the key format</param>
/// <param name="values">the ref string will contain all the values of the namevaluecollection</param>
public static void ConvertFromNameValueCollection(NameValueCollection nvc, ref string keys, ref string values)
{
if(nvc == null || nvc.Count == 0)
return;
var sbKey = new StringBuilder();
var sbValue = new StringBuilder();
var index = 0;
foreach(var key in nvc.AllKeys)
{
if(key.IndexOf(':') != -1)
throw new ArgumentException("ExtendedAttributes Key can not contain the character \":\"");
var v = nvc[key];
if(!string.IsNullOrEmpty(v))
{
sbKey.Append($"{key}:S:{index}:{v.Length}:");
sbValue.Append(v);
index += v.Length;
}
}
keys = sbKey.ToString();
values = sbValue.ToString();
}
public static void ConvertFromDictionary(Dictionary<string, string> nvc, ref string keys, ref string values)
{
if (nvc == null || nvc.Count == 0)
return;
var sbKey = new StringBuilder();
var sbValue = new StringBuilder();
var index = 0;
foreach (var key in nvc.Keys)
{
if (key.IndexOf(':') != -1)
throw new ArgumentException("ExtendedAttributes Key can not contain the character \":\"");
var v = nvc[key];
if (!string.IsNullOrEmpty(v))
{
sbKey.Append($"{key}:S:{index}:{v.Length}:");
sbValue.Append(v);
index += v.Length;
}
}
keys = sbKey.ToString();
values = sbValue.ToString();
}
}
}