My favorites | Sign in
Project Logo
                
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.Reflection.Emit;
using System.Reflection;

namespace System.Collections.Generic
{
/// <summary>
/// Implements a <see cref="Dictionary{TKey, TValue}"/> that can be safely
/// serialized to XML and deserialized back, preserving type information.
/// </summary>
/// <remarks>
/// The serialization format will attempt to write the minimal information possible.
/// Typical format is as follows:
/// <code>
/// <dictionary>
/// <entry>
/// <key>foo</key>
/// <value>25</value>
/// </entry>
/// <entry>
/// <key>bar</key>
/// <value>30</value>
/// </entry>
/// </dictionary>
/// </code>
/// The type of the key and the value are the same as the ones for
/// <typeparamref name="TKey"/> and <typeparamref name="TValue"/>.
/// If the type of a value for either one is a derived type, the
/// type information will be written in a <c>type</c> attribute,
/// which will be used to deserialize the XML with the appropriate
/// <see cref="XmlSerializer"/>. The serialized type name does not
/// include assembly version information, to make it more version-resilient.
/// </remarks>
[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
#if NET20
internal delegate T Func<T>();
#endif

private static readonly Dictionary<CacheKey, Func<XmlSerializer>> keySerializers = new Dictionary<CacheKey, Func<XmlSerializer>>();
private static readonly Dictionary<CacheKey, Func<XmlSerializer>> valueSerializers = new Dictionary<CacheKey, Func<XmlSerializer>>();

private static readonly XmlSerializerFactory serializerFactory = new XmlSerializerFactory();
private static readonly XmlSerializerNamespaces serializerNamespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") });

/// <summary>
/// Initializes an instance of the dictionary, setting the
/// <see cref="XmlRoot"/> namespace to an empty string.
/// </summary>
public SerializableDictionary()
{
XmlRoot = new XmlRootAttribute("dictionary") { Namespace = "" };
}

/// <summary>
/// Allows overriding of the xml namespace used to serialize
/// child elements.
/// </summary>
public XmlRootAttribute XmlRoot { get; set; }

/// <summary>
/// Tests whether an extensions dictionary can be read from the current
/// <see cref="XmlReader"/> position using the default empty XML namespace Uri.
/// </summary>
/// <remarks>
/// If the reader is in the <see cref="ReadState.Initial"/>, it's advanced
/// to the content for the check.
/// </remarks>
public static bool CanRead(XmlReader reader)
{
return CanRead(reader, new XmlRootAttribute("dictionary"));
}

/// <summary>
/// Tests whether an extensions dictionary can be read from the current
/// <see cref="XmlReader"/> position using the given root element information.
/// </summary>
/// <remarks>
/// If the reader is in the <see cref="ReadState.Initial"/>, it's advanced
/// to the content for the check.
/// </remarks>
public static bool CanRead(XmlReader reader, XmlRootAttribute xmlRoot)
{
if (reader.ReadState == ReadState.Initial)
reader.MoveToContent();

return reader.NodeType == XmlNodeType.Element &&
reader.LocalName == xmlRoot.ElementName &&
reader.NamespaceURI == xmlRoot.Namespace;
}

/// <summary>
/// Reads the dictionary using the default root element name and namespace.
/// </summary>
public static SerializableDictionary<TKey, TValue> ReadXml(XmlReader reader)
{
return ReadXml(reader, new XmlRootAttribute("dictionary"));
}

/// <summary>
/// Reads the dictionary using the given root element override.
/// </summary>
public static SerializableDictionary<TKey, TValue> ReadXml(XmlReader reader, XmlRootAttribute xmlRoot)
{
if (!CanRead(reader, xmlRoot))
XmlExceptions.ThrowXmlException(String.Format(
"Unexpected element <{0} xmlns='{1}' ...>.",
reader.LocalName, reader.NamespaceURI),
reader);

var extensions = new SerializableDictionary<TKey, TValue>();
((IXmlSerializable)extensions).ReadXml(reader);

return extensions;
}

public void WriteXml(XmlWriter writer)
{
writer.WriteStartElement(XmlRoot.ElementName, XmlRoot.Namespace);
((IXmlSerializable)this).WriteXml(writer);
writer.WriteEndElement();
}

void IXmlSerializable.ReadXml(XmlReader reader)
{
var rootNamespaceUri = reader.NamespaceURI;
if (reader.ReadToDescendant("entry", rootNamespaceUri))
{
var depth = reader.Depth;
do
{
using (var entry = reader.ReadSubtree())
{
if (reader.ReadToDescendant("key", rootNamespaceUri))
{
bool skip = false;
var tKey = ReadType(reader, typeof(TKey), out skip);
if (skip) continue;

var keySerializer = GetSerializer(keySerializers, tKey, new XmlRootAttribute("key") { Namespace = rootNamespaceUri });
var key = keySerializer.Deserialize(reader.ReadSubtree());

if (reader.ReadToNextSibling("value", rootNamespaceUri))
{
var tValue = ReadType(reader, typeof(TValue), out skip);
if (skip) continue;

var valueSerializer = GetSerializer(valueSerializers, tValue, new XmlRootAttribute("value") { Namespace = rootNamespaceUri });
var value = valueSerializer.Deserialize(reader.ReadSubtree());

Add((TKey)key, (TValue)value);
}
else
{
Add((TKey)key, default(TValue));
}
}
}
} while (reader.Read() && reader.MoveToContent() == XmlNodeType.Element && reader.Depth >= depth);
}
}

void IXmlSerializable.WriteXml(XmlWriter writer)
{
var tKey = typeof(TKey);
var tValue = typeof(TValue);
foreach (var item in this)
{
try
{
// Ensure we can create the serializers first for the types
var keyType = item.Key.GetType();
var valueType = item.Value != null ? item.Value.GetType() : tValue;

// Optimize if we know the data is not serializable up front.
if (!
(keyType.IsPublic || keyType.IsNestedPublic) &&
(valueType.IsPublic || valueType.IsNestedPublic))
continue;

var keyWriter = keyType == tKey ? writer : new TypeWriter(writer, keyType);
keyWriter = new NonXsiXmlWriter(keyWriter);
var keySerializer = GetSerializer(keySerializers, keyType, new XmlRootAttribute("key") { Namespace = XmlRoot.Namespace });

if (item.Value != null)
{
var valueWriter = valueType == tValue ? writer : new TypeWriter(writer, valueType);
valueWriter = new NonXsiXmlWriter(valueWriter);
var valueSerializer = GetSerializer(valueSerializers, valueType, new XmlRootAttribute("value") { Namespace = XmlRoot.Namespace });

writer.WriteStartElement("entry");
// Serialize Key
keySerializer.Serialize(keyWriter, item.Key, serializerNamespaces);
keyWriter.Flush();

// Serialize Value
valueSerializer.Serialize(valueWriter, item.Value, serializerNamespaces);
valueWriter.Flush();

// Make sure we close the entry tag.
writer.WriteEndElement();
}
else
{
writer.WriteStartElement("entry");
// Serialize Key
keySerializer.Serialize(keyWriter, item.Key, serializerNamespaces);
keyWriter.Flush();

// Make sure we close the entry tag.
writer.WriteEndElement();
}
}
catch (Exception)
{
}
}
}

System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
{
throw new NotImplementedException();
}

private Type ReadType(XmlReader reader, Type defaultType, out bool shouldSkip)
{
var result = defaultType;
shouldSkip = false;

var typeName = reader.GetAttribute("type");
if (!String.IsNullOrEmpty(typeName))
{
var type = Type.GetType(typeName);

if (type == null || !defaultType.IsAssignableFrom(type) ||
!(type.IsPublic || type.IsNestedPublic))
shouldSkip = true;

if (type != null)
result = type;
}

return result;
}

private XmlSerializer GetSerializer(Dictionary<CacheKey, Func<XmlSerializer>> cache, Type forType, XmlRootAttribute root)
{
Func<XmlSerializer> factory;
var key = new CacheKey { Type = forType, Root = root };
if (!cache.TryGetValue(key, out factory))
{
var serializer = serializerFactory.CreateSerializer(forType, root);
factory = BuildSerializerFactory(serializer.GetType());
cache[key] = factory;
}

return factory();
}

private Func<XmlSerializer> BuildSerializerFactory(Type serializerType)
{
// generate new serializerType() anonymous factory function.
var method = new DynamicMethod("KeyString", serializerType, null);

// Preparing Reflection instances
var ctor = serializerType.GetConstructor(
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
null, new Type[] { }, null);
// Setting return type
// Adding parameters
ILGenerator gen = method.GetILGenerator();
// Preparing locals
LocalBuilder lb = gen.DeclareLocal(serializerType);
// Preparing labels
Label label9 = gen.DefineLabel();
// Writing body
gen.Emit(OpCodes.Nop);
gen.Emit(OpCodes.Newobj, ctor);
gen.Emit(OpCodes.Stloc_0);
gen.Emit(OpCodes.Br_S, label9);
gen.MarkLabel(label9);
gen.Emit(OpCodes.Ldloc_0);
gen.Emit(OpCodes.Ret);
// finished

return (Func<XmlSerializer>)method.CreateDelegate(typeof(Func<XmlSerializer>));
}

private class TypeWriter : XmlWrappingWriter
{
bool root = true;
Type type;

public TypeWriter(XmlWriter baseWriter, Type type)
: base(baseWriter)
{
this.type = type;
}

public override void WriteStartElement(string prefix, string localName, string ns)
{
base.WriteStartElement(prefix, localName, ns);

if (root)
{
var typeName = Type.GetType(type.FullName) != null ?
type.FullName :
type.FullName + ", " + type.Assembly.FullName.Substring(0, type.Assembly.FullName.IndexOf(",")).Trim();

base.WriteAttributeString("type", typeName);
root = false;
}
}
}

private class NonXsiXmlWriter : XmlWrappingWriter
{
bool skip = false;

public NonXsiXmlWriter(XmlWriter baseWriter)
: base(baseWriter)
{
}

public override void WriteStartAttribute(string prefix, string localName, string ns)
{
if (prefix == "xmlns" && (localName == "xsd" || localName == "xsi"))
{
skip = true;
return;
}

base.WriteStartAttribute(prefix, localName, ns);
}

public override void WriteString(string text)
{
if (skip)
return;

base.WriteString(text);
}

public override void WriteEndAttribute()
{
if (skip)
{
skip = false;
return;
}

base.WriteEndAttribute();
}
}

private static class XmlExceptions
{
internal static void ThrowIfAttributeMissing(string attributeName, XmlReader reader)
{
if (String.IsNullOrEmpty(reader.GetAttribute(attributeName)))
{
ThrowXmlException(
String.Format(
"Attribute '{0}' is required.",
attributeName),
reader);
}
}

internal static void ThrowXmlException(string message, XmlReader reader)
{
int lineNumber = -1;
int linePosition = -1;

var info = reader as IXmlLineInfo;
if (info != null && info.HasLineInfo())
{
lineNumber = info.LineNumber;
linePosition = info.LinePosition;
}

var summary = new StringBuilder();
if (reader.NodeType != XmlNodeType.Element)
reader.MoveToElement();

using (XmlWriter w = XmlWriter.Create(summary, new XmlWriterSettings { OmitXmlDeclaration = true }))
{
w.WriteNode(new SummaryXmlReader(
reader.LocalName, reader.NamespaceURI, reader.ReadSubtree()),
false);
}

throw new XmlException(
String.Format(
@"{0}
There is an error in the XML document or fragment:
{1}",
message,
summary.ToString().Substring(0, summary.ToString().Length - 2).Trim() + ">"),
null,
lineNumber,
linePosition);
}

class SummaryXmlReader : XmlWrappingReader
{
string emptyLocalName;
string namespaceUri;
bool eof;

public SummaryXmlReader(string emptyLocalName, string namespaceUri, XmlReader baseReader)
: base(baseReader)
{
this.emptyLocalName = emptyLocalName;
this.namespaceUri = namespaceUri;
}

public new XmlReader MoveToContent()
{
base.MoveToContent();
return this;
}

public override bool Read()
{
if (LocalName == emptyLocalName &&
NamespaceURI == namespaceUri &&
NodeType == XmlNodeType.Element)
{
eof = true;
}

return !eof && base.Read();
}

public override bool IsEmptyElement
{
get
{
if (LocalName == emptyLocalName && NamespaceURI == namespaceUri)
return true;
else
return base.IsEmptyElement;
}
}
}
}

private class CacheKey
{
public Type Type;
public XmlRootAttribute Root;

public override bool Equals(object obj)
{
if (Object.ReferenceEquals(this, obj)) return true;

var x = this;
var y = obj as CacheKey;

if (Object.Equals(null, y)) return false;

return x.Type == y.Type &&
x.Root.ElementName == y.Root.ElementName &&
x.Root.Namespace == y.Root.Namespace;
}

public override int GetHashCode()
{
return this.Type.GetHashCode() ^ this.Root.ElementName.GetHashCode() ^ this.Root.Namespace.GetHashCode();
}
}
}
}
Show details Hide details

Change log

r86 by kzu.net on Dec 01, 2008   Diff
Fixed broken serialization
Go to: 
Project members, sign in to write a code review

Older revisions

r84 by kzu.net on Nov 30, 2008   Diff
Optimized serialization by omitting
<value> tag if the value is null.
r83 by kzu.net on Nov 28, 2008   Diff
Improved performance of writing the
SerializableDictionary when the key or
value is not a public type (hence it
cannot be written or read)
r82 by kzu.net on Nov 28, 2008   Diff
Improved performance of writing the
SerializableDictionary when the key or
value is not a public type (hence it
cannot be written or read)
All revisions of this file

File info

Size: 14104 bytes, 479 lines
Hosted by Google Code