/
GenericSerializer.cs
118 lines (98 loc) · 3.17 KB
/
GenericSerializer.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
using System;
using System.Xml;
using System.Reflection;
namespace Mueller.Wddx
{
/// <summary>
/// Provides serialization services for any object not covered by a pre-defined serializer.
/// </summary>
/// <remarks>
/// <para>If the object has properties or fields, they will be serialized to a WDDX struct.
/// If not, a call to the object's <c>ToString()</c> method will be serialized as a string
/// value.</para>
///
/// <para>This class is a Singleton class - only one instance of it will ever exist
/// in a given AppDomain.</para>
/// </remarks>
/// <seealso cref="IWddxObjectSerializer"/>
internal class GenericSerializer : IWddxObjectSerializer
{
private static GenericSerializer instance = null;
/// <summary>
/// Provides access to the instance of this class.
/// </summary>
public static GenericSerializer Instance
{
get
{
if (instance == null)
{
lock (typeof(GenericSerializer))
{
if (instance == null)
instance = new GenericSerializer();
}
}
return instance;
}
}
private GenericSerializer() {}
/// <summary>
/// Writes the serialized object to the XML stream.
/// </summary>
/// <param name="output">A pre-initialized <see cref="XmlTextWriter"/> object.</param>
/// <param name="obj">Object to serialize.</param>
public void WriteObject(XmlWriter output, object obj)
{
if (obj == null)
throw new WddxException("Attemped to serialize incompatible object: null.");
Type objType = obj.GetType();
PropertyInfo[] objProperties = objType.GetProperties();
FieldInfo[] objFields = objType.GetFields();
if (objProperties.Length == 0 && objFields.Length == 0)
{
output.WriteElementString("string", obj.ToString());
}
else
{
if (objProperties.Length > 0)
{
WddxObjectSerializerFactory factory = new WddxObjectSerializerFactory();
output.WriteStartElement("", "struct", "");
output.WriteAttributeString("type", objType.FullName);
object propertyValue;
foreach (PropertyInfo property in objProperties)
{
if (property.CanRead)
{
output.WriteStartElement("var");
output.WriteAttributeString("name", property.Name);
propertyValue = property.GetValue(obj, null);
factory.GetSerializer(propertyValue).WriteObject(output, propertyValue);
output.WriteEndElement();
}
}
output.WriteEndElement();
}
if (objFields.Length > 0)
{
WddxObjectSerializerFactory factory = new WddxObjectSerializerFactory();
output.WriteStartElement("struct");
object fieldValue;
foreach (FieldInfo field in objFields)
{
if (field.IsPublic)
{
output.WriteStartElement("var");
output.WriteAttributeString("name", field.Name);
fieldValue = field.GetValue(obj);
factory.GetSerializer(fieldValue).WriteObject(output, fieldValue);
output.WriteEndElement();
}
}
output.WriteEndElement();
}
}
}
}
}