This repository has been archived by the owner on Aug 25, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
BaseClassSerializable.cs
503 lines (414 loc) · 18.7 KB
/
BaseClassSerializable.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using Newtonsoft.Json;
using Formatting = Newtonsoft.Json.Formatting;
namespace FatturaElettronica.Common
{
/// <summary>
/// - XML (de)serialization;
/// - JSON (de)serialization.
/// </summary>
public class BaseClassSerializable : BaseClass, IXmlSerializable
{
/// <summary>
/// Constructor.
/// </summary>
protected BaseClassSerializable()
{
XmlOptions = new XmlOptions()
{
DateTimeFormat = "yyyy-MM-dd",
DecimalFormat = "0.00######"
};
}
protected BaseClassSerializable(XmlReader r) : base() { ReadXml(r); }
protected XmlOptions XmlOptions { get; set; }
readonly Stack<JsonProperty> _stack = new Stack<JsonProperty>();
/// <summary>
/// Deserializes the current BusinessObject from a json stream.
/// </summary>
/// <param name="r">Active json stream reader</param>
/// <remarks>Side effects on parse handling</remarks>
public virtual void FromJson(JsonReader r)
{
_stack.Clear();
r.FloatParseHandling = FloatParseHandling.Decimal;
JsonProperty current;
Type objectType, elementType;
PropertyInfo prop = null;
while (r.Read())
{
switch (r.TokenType)
{
case JsonToken.StartObject:
current = null;
if (_stack.Count() > 0)
current = _stack.Peek();
if (current != null)
{
current.Child = prop;
objectType = current.Child.PropertyType;
if (objectType.IsGenericList())
{
elementType = objectType.GetTypeInfo().GenericTypeArguments.Single();
var newObject = Activator.CreateInstance(elementType);
var add = objectType.GetMethod("Add");
try
{
add.Invoke(current.Value, new[] { newObject });
}
catch (Exception)
{
throw new JsonParseException($"Unexpected element type {elementType}", r);
}
current = new JsonProperty(newObject);
}
else
{
if (!objectType.IsSubclassOfBusinessObject())
throw new JsonParseException($"Unexpected property type {objectType.FullName}", r);
current = new JsonProperty(current.Child.GetValue(current.Value, null));
}
}
else
current = new JsonProperty(this);
_stack.Push(current);
break;
case JsonToken.EndObject:
if (_stack.Count > 0)
_stack.Pop();
// Restore parent property from stack
prop = null;
if (_stack.Count > 1)
prop = _stack.ElementAt(1).Child;
break;
case JsonToken.PropertyName:
if (_stack.Count() == 0)
throw new JsonParseException($"Malformed JSON", r);
current = _stack.Peek();
var name = (string)r.Value;
prop = GetPropertyInfo((BaseClassSerializable)current.Value, name);
if (prop == null)
throw new JsonParseException($"Unexpected property {name}", r);
current.Child = prop;
break;
case JsonToken.StartArray:
current = null;
if (_stack.Count() > 0)
current = _stack.Peek();
if (current != null && current.Child != null)
{
objectType = current.Child.PropertyType;
if (!objectType.IsGenericList())
throw new JsonParseException($"Unexpected property type {objectType.FullName}", r);
elementType = objectType.GetTypeInfo().GenericTypeArguments.Single();
var value = current.Child.GetValue(current.Value, null);
var clear = objectType.GetMethod("Clear");
clear.Invoke(value, null);
current = new JsonProperty(current.Child, value);
}
else
current = new JsonProperty(this);
_stack.Push(current);
break;
case JsonToken.EndArray:
if (_stack.Count > 0)
_stack.Pop();
// Restore parent property from stack
prop = null;
if (_stack.Count > 1)
prop = _stack.ElementAt(1).Child;
break;
case JsonToken.Integer:
case JsonToken.Float:
case JsonToken.String:
case JsonToken.Boolean:
case JsonToken.Null:
case JsonToken.Date:
current = null;
if (_stack.Count() > 0)
current = _stack.Peek();
if (current != null)
{
objectType = prop.PropertyType;
if (current.Value.GetType().IsGenericList())
{
elementType = objectType.GetTypeInfo().GenericTypeArguments.Single();
var add = objectType.GetMethod("Add");
var value = Cast(elementType, r.Value);
try
{
add.Invoke(current.Value, new[] { value });
}
catch (Exception)
{
throw new JsonParseException($"Unexpected element type {elementType}", r);
}
}
else
{
var value = Cast(objectType, r.Value);
current.Child.SetValue(current.Value, value, null);
}
}
else
throw new JsonParseException($"Malformed JSON", r);
break;
default:
throw new JsonParseException($"Unexpected token {r.TokenType}", r);
}
}
}
/// <summary>
/// Helper method to cast json properties
/// </summary>
/// <param name="target">target type</param>
/// <param name="value">source value</param>
/// <returns></returns>
private static object Cast(Type target, object value)
{
if (target == typeof(Int32) || target == typeof(Int32?))
return Convert.ToInt32(value);
if (target == typeof(Decimal) || target == typeof(Decimal?))
return Convert.ToDecimal(value);
return value;
}
/// <summary>
/// Helper method to get a named property
/// </summary>
/// <param name="value"></param>
/// <param name="name"></param>
/// <returns></returns>
private static PropertyInfo GetPropertyInfo(BaseClassSerializable value, string name)
{
var type = value.GetType();
var properties = value.GetAllDataProperties().ToList();
// XmlElementAttribute comes first
var property = properties
.Where(prop => prop.GetCustomAttributes(typeof(XmlElementAttribute), false)
.Where(ca => ((XmlElementAttribute)ca).ElementName.Equals(name, StringComparison.OrdinalIgnoreCase))
.Any())
.FirstOrDefault();
// Fallback to property name
if (property == null)
property = properties.FirstOrDefault(n => n.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
return property;
}
/// <summary>
/// Serializes the instance to JSON
/// </summary>
/// <returns>A JSON string representing the class instance.</returns>
public virtual string ToJson()
{
return ToJson(JsonOptions.None);
}
/// <summary>
/// Serializes the class to JSON.
/// </summary>
/// <param name="jsonOptions">JSON formatting options.</param>
/// <returns>A JSON string representing the class instance.</returns>
public virtual string ToJson(JsonOptions jsonOptions)
{
var json = JsonConvert.SerializeObject(
this, (jsonOptions == JsonOptions.Indented) ? Formatting.Indented : Formatting.None,
new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Serialize, DefaultValueHandling = DefaultValueHandling.Ignore });
return json;
}
public XmlSchema GetSchema() { return null; }
/// <summary>
/// Serializes the current BusinessObject instance to a XML stream.
/// </summary>
/// <param name="w">Active XML stream writer.</param>
/// <remarks>Writes only its inner content, not the outer element. Leaves the writer at the same depth.</remarks>
public virtual void WriteXml(XmlWriter w)
{
foreach (var property in GetAllDataProperties())
{
var attribute = property.GetCustomAttributes(typeof(XmlElementAttribute), false)
.Cast<XmlElementAttribute>()
.FirstOrDefault();
var propertyName = attribute == null ? property.Name : attribute.ElementName;
var value = property.GetValue(this, null);
if (value == null && !XmlOptions.SerializeNullValues) continue;
var child = value as BaseClassSerializable;
if (child != null)
{
if (child.IsEmpty() && XmlOptions.SerializeEmptyBusinessObjects == false) continue;
w.WriteStartElement(propertyName);
child.WriteXml(w);
w.WriteEndElement();
continue;
}
if (property.PropertyType.IsGenericList())
{
WriteXmlList(propertyName, value, w);
continue;
}
if (value is string)
{
if (!string.IsNullOrEmpty(value.ToString()) || XmlOptions.SerializeEmptyStrings)
{
w.WriteElementString(propertyName, value.ToString());
}
continue;
}
if (value is DateTime && XmlOptions.DateTimeFormat != null && !property.GetCustomAttributes<IgnoreXmlDateFormat>().Any())
{
w.WriteElementString(propertyName, ((DateTime)value).ToString(XmlOptions.DateTimeFormat));
continue;
}
if (value is decimal && XmlOptions.DecimalFormat != null)
{
w.WriteElementString(propertyName, ((decimal)value).ToString(XmlOptions.DecimalFormat, CultureInfo.InvariantCulture));
continue;
}
// all else fail so just let the value flush straight to XML.
w.WriteStartElement(propertyName);
if (value != null)
{
w.WriteValue(value);
}
w.WriteEndElement();
}
}
/// <summary>
/// Deserializes a List of BusinessObject or strings to one or more XML elements.
/// </summary>
/// <param name="propertyName">Property name.</param>
/// <param name="value">Property value.</param>
/// <param name="w">Active XML stream writer.</param>
private static void WriteXmlList(string propertyName, object value, XmlWriter w)
{
var type = value.GetType();
var e = type.GetMethod("GetEnumerator").Invoke(value, null) as IEnumerator;
while (e != null && e.MoveNext())
{
if (e.Current == null) continue;
var current = e.Current;
w.WriteStartElement(propertyName);
{
if (current is BaseClassSerializable)
{
((BaseClassSerializable)current).WriteXml(w);
}
else if (current is string)
{
w.WriteString((string)current);
}
else
{
w.WriteValue(e.Current);
}
}
w.WriteEndElement();
}
}
/// <summary>
/// Deserializes the current BusinessObject from a XML stream.
/// </summary>
/// <param name="r">Active XML stream reader.</param>
/// <remarks>Reads the outer element. Leaves the reader at the same depth.</remarks>
public virtual void ReadXml(XmlReader r)
{
var isEmpty = r.IsEmptyElement;
r.ReadStartElement();
if (isEmpty) return;
var properties = GetAllDataProperties().ToList();
while (r.NodeType == XmlNodeType.Element)
{
var property = properties
.Where(prop => prop.GetCustomAttributes(typeof(XmlElementAttribute), false)
.Where(ca => ((XmlElementAttribute)ca).ElementName == r.Name).Any())
.FirstOrDefault();
if (property == null)
property = properties.FirstOrDefault(n => n.Name.Equals(r.Name));
if (property == null)
{
r.Skip();
continue;
}
var type = property.PropertyType;
var value = property.GetValue(this, null);
if (type.IsSubclassOfBusinessObject())
{
((BaseClassSerializable)value).ReadXml(r);
continue;
}
// if property type is List<T>, try to fetch the list from XML.
if (type.IsGenericList())
{
ReadXmlList(value, type, r.Name, r);
continue;
}
// ReadElementContentAs won't accept a nullable type.
if (type == typeof(DateTime?)) type = typeof(DateTime);
if (type == typeof(decimal?)) type = typeof(decimal);
if (type == typeof(int?)) type = typeof(int);
property.SetValue(this, r.ReadElementContentAs(type, null), null);
}
if (r.NodeType == XmlNodeType.Text) r.Skip();
r.ReadEndElement();
}
/// <summary>
/// Serializes one or more XML elements into a List of BusinessObjects.
/// </summary>
/// <param name="propertyValue">Property value. Must be a List of BusinessObject instances.</param>
/// <param name="propertyValue">Property type</param>
/// <param name="elementName">Element name.</param>
/// <param name="r">Active XML stream reader.</param>
private static void ReadXmlList(object propertyValue, Type propertyType, string elementName, XmlReader r)
{
var argumentType = propertyType.GetTypeInfo().GenericTypeArguments.Single();
// note that the 'canonical' call to GetRuntimeMethod returns null for some reason,
// see http://stackoverflow.com/questions/21307845/runtimereflectionextensions-getruntimemethod-does-not-work-as-expected
//var method = propertyType.GetRuntimeMethod("Clear", new[] { propertyType });
var clear = propertyType.GetMethod("Clear");
clear.Invoke(propertyValue, null);
var add = propertyType.GetMethod("Add");
while (r.NodeType == XmlNodeType.Element && r.Name == elementName)
{
if (argumentType.IsSubclassOfBusinessObject())
{
// list items are expected to be of BusinessObject type.
var bo = Activator.CreateInstance(argumentType);
((BaseClassSerializable)bo).ReadXml(r);
add.Invoke(propertyValue, new[] { bo });
continue;
}
if (argumentType == typeof(string))
{
add.Invoke(propertyValue, new[] { r.ReadElementContentAsString() });
continue;
}
if (argumentType == typeof(int))
{
add.Invoke(propertyValue, new[] { r.ReadElementContentAs(argumentType, null) });
}
}
}
private class JsonProperty
{
public PropertyInfo Child { get; set; }
public object Value { get; set; }
public JsonProperty(PropertyInfo property, object value)
{
this.Child = property;
this.Value = value;
}
public JsonProperty(object value) : this(null, value)
{
}
public override string ToString()
{
return String.Format("Child {0} - Value {1}", Child, Value);
}
}
}
}