-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBasicXmlSerializer.cs
More file actions
364 lines (331 loc) · 14.7 KB
/
Copy pathBasicXmlSerializer.cs
File metadata and controls
364 lines (331 loc) · 14.7 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml.Linq;
using System.Diagnostics;
#if !(PCL || STANDARD || CORE)
namespace System.Runtime.Serialization
{
public class BasicXmlSerializer : BasicSerializer<XElement>
{
public override TOuptutType Deserialize<TOuptutType>(XElement element)
{
return (TOuptutType)Deserialize(typeof(TOuptutType), element);
}
public T Deserialize<T>(string xml)
{
return (T)Deserialize(typeof(T), XElement.Parse(xml));
}
public T Deserialize<T>(XDocument document)
{
return (T)Deserialize(typeof(T), document);
}
public object Deserialize(Type type, XDocument document)
{
var nodeName = type.Name;
var element = document.Element(nodeName);
if (element == null) throw new SerializationException(string.Format("No child node with name '{0}' found", nodeName));
return Deserialize(type, element);
}
public object Deserialize(Type type, XElement element)
{
var nodeName = type.Name;
if (element.Name != nodeName)
{
//throw new SerializationException(string.Format("Element is not named '{0}'", nodeName));
}
object item;
try
{
#if WindowsCE
// CF doesn't support 'Activator.CreateInstance' with two parameters
var ctor = type.GetDefaultConstructor(true);
item = ctor.Invoke(null);
#else
item = Activator.CreateInstance(type, true);
#endif
}
catch (Exception ex)
{
throw new SerializationException(string.Format("Unable to create instance of type '{0}'. Is there a parameterless constructor?", type.Name), ex);
}
var flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
var props = item.GetType().GetProperties(flags);
string intermediate;
foreach (var prop in props)
{
var attrib = element.Attribute(prop.Name);
if (attrib != null)
{
if (prop.PropertyType.IsEnum)
{
prop.SetValue(item, Enum.Parse(prop.PropertyType, attrib.Value, true), null);
}
else
{
string typeName;
if (prop.PropertyType.IsNullable())
{
typeName = prop.PropertyType.GetGenericArguments()[0].Name;
if (attrib.Value.IsNullOrEmpty())
{
if (typeName == "String")
{
prop.SetValue(item, attrib.Value, null);
}
else
{
prop.SetValue(item, null, null);
}
continue;
}
}
else
{
typeName = prop.PropertyType.Name;
}
switch (typeName)
{
case "String":
prop.SetValue(item, attrib.Value, null);
break;
case "Boolean":
prop.SetValue(item, bool.Parse(attrib.Value), null);
break;
case "DateTime":
prop.SetValue(item, DateTime.Parse(attrib.Value), null);
break;
case "TimeSpan":
prop.SetValue(item, TimeSpan.Parse(attrib.Value), null);
break;
case "Byte":
intermediate = attrib.Value;
if (attrib.Value.StartsWith("0x", StringComparison.CurrentCultureIgnoreCase))
{
prop.SetValue(item, byte.Parse(intermediate, System.Globalization.NumberStyles.HexNumber), null);
}
else
{
prop.SetValue(item, byte.Parse(intermediate), null);
}
break;
case "Single":
intermediate = attrib.Value;
if (attrib.Value.StartsWith("0x", StringComparison.CurrentCultureIgnoreCase))
{
prop.SetValue(item, Single.Parse(intermediate, System.Globalization.NumberStyles.HexNumber), null);
}
else
{
prop.SetValue(item, Single.Parse(intermediate), null);
}
break;
case "Double":
intermediate = attrib.Value;
if (attrib.Value.StartsWith("0x", StringComparison.CurrentCultureIgnoreCase))
{
prop.SetValue(item, double.Parse(intermediate, System.Globalization.NumberStyles.HexNumber), null);
}
else
{
prop.SetValue(item, double.Parse(intermediate), null);
}
break;
case "Int16":
intermediate = attrib.Value;
if (attrib.Value.StartsWith("0x", StringComparison.CurrentCultureIgnoreCase))
{
prop.SetValue(item, short.Parse(intermediate, System.Globalization.NumberStyles.HexNumber), null);
}
else
{
prop.SetValue(item, short.Parse(intermediate), null);
}
break;
case "UInt16":
intermediate = attrib.Value;
if (attrib.Value.StartsWith("0x", StringComparison.CurrentCultureIgnoreCase))
{
prop.SetValue(item, ushort.Parse(intermediate, System.Globalization.NumberStyles.HexNumber), null);
}
else
{
prop.SetValue(item, ushort.Parse(intermediate), null);
}
break;
case "Int32":
intermediate = attrib.Value;
if (attrib.Value.StartsWith("0x", StringComparison.CurrentCultureIgnoreCase))
{
prop.SetValue(item, int.Parse(intermediate, System.Globalization.NumberStyles.HexNumber), null);
}
else
{
prop.SetValue(item, int.Parse(intermediate), null);
}
break;
case "UInt32":
intermediate = attrib.Value;
if (attrib.Value.StartsWith("0x", StringComparison.CurrentCultureIgnoreCase))
{
prop.SetValue(item, uint.Parse(intermediate, System.Globalization.NumberStyles.HexNumber), null);
}
else
{
prop.SetValue(item, uint.Parse(intermediate), null);
}
break;
case "Int64":
intermediate = attrib.Value;
if (attrib.Value.StartsWith("0x", StringComparison.CurrentCultureIgnoreCase))
{
prop.SetValue(item, long.Parse(intermediate, System.Globalization.NumberStyles.HexNumber), null);
}
else
{
prop.SetValue(item, long.Parse(intermediate), null);
}
break;
case "UInt64":
intermediate = attrib.Value;
if (attrib.Value.StartsWith("0x", StringComparison.CurrentCultureIgnoreCase))
{
prop.SetValue(item, ulong.Parse(intermediate, System.Globalization.NumberStyles.HexNumber), null);
}
else
{
prop.SetValue(item, ulong.Parse(intermediate), null);
}
break;
default:
throw new SerializationException("Unsupported property type: " + prop.PropertyType.Name);
}
}
}
else
{
var child = element.Element(prop.Name);
if (child != null)
{
//var childType = Type.GetType();
var p = Deserialize(prop.PropertyType, child);
prop.SetValue(item, p, null);
}
}
}
return item;
}
public override XElement Serialize(object source, bool includeNonPublicProperties)
{
return Serialize(source, null, includeNonPublicProperties);
}
public XElement Serialize(object source, string objectName, bool includeNonPublicProperties)
{
XElement element;
var flags = BindingFlags.Instance | BindingFlags.Public;
if(includeNonPublicProperties)
{
flags |= BindingFlags.NonPublic;
}
var props = source.GetType().GetProperties(flags);
var type = source.GetType();
string nodeName;
if(objectName == null)
{
if (type.IsGenericType)
{
nodeName = type.Name.CropAtLast('`');
}
else
{
nodeName = type.Name;
}
}
else
{
nodeName = objectName;
}
element = new XElement(nodeName);
foreach (var prop in props)
{
string name = prop.Name;
string value = null;
bool valIsElement = false;
if (!prop.CanRead) continue;
if (prop.GetCustomAttributes(typeof(DoNotSerializeAttribute), true).Count() > 0) continue;
if(prop.PropertyType.IsEnum)
{
value = prop.GetValue(source, null).ToString();
}
else if (prop.PropertyType.IsArray)
{
throw new NotSupportedException();
}
else
{
string typeName;
if (prop.PropertyType.IsNullable())
{
typeName = prop.PropertyType.GetGenericArguments()[0].Name;
}
else
{
typeName = prop.PropertyType.Name;
}
switch (typeName)
{
case "String":
case "Boolean":
case "Byte":
case "TimeSpan":
case "Single":
case "Double":
case "Int16":
case "UInt16":
case "Int32":
case "UInt32":
case "Int64":
case "UInt64":
value = (prop.GetValue(source, null) ?? string.Empty).ToString();
break;
case "DateTime":
try
{
var tempDT = Convert.ToDateTime(prop.GetValue(source, null));
if (tempDT == DateTime.MinValue) continue;
value = tempDT.ToString("MM/dd/yyyy HH:mm:ss.fffffff");
}
catch (Exception ex)
{
Debug.WriteLine("BasicXmlSerializer: Error parsing DateTime: " + ex.Message);
continue;
}
break;
default:
var o = prop.GetValue(source, null);
XElement child;
if (o == null)
{
child = new XElement(prop.Name);
}
else
{
child = Serialize(o, prop.Name, includeNonPublicProperties);
}
element.Add(child);
valIsElement = true;
break;
}
}
if ((!valIsElement) && (value != null))
{
element.AddAttribute(name, value);
}
}
return element;
}
}
}
#endif