Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="cdmdotnet Limited">
4 : // // Copyright cdmdotnet Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using MongoDB.Bson;
11 : using MongoDB.Bson.Serialization;
12 :
13 : namespace Cqrs.MongoDB.Serialisers
14 : {
15 : public class TypeSerialiser : IBsonSerializer<Type>
16 0 : {
17 : #region Implementation of IBsonSerializer
18 :
19 : /// <summary>
20 : /// Deserializes a value.
21 : /// </summary>
22 : /// <param name="context">The deserialization context.</param>
23 : /// <param name="args">The deserialization args.</param>
24 : /// <returns>
25 : /// A deserialized value.
26 : /// </returns>
27 1 : public object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
28 : {
29 : return ((IBsonSerializer<Type>) this).Deserialize(context, args);
30 : }
31 :
32 : /// <summary>
33 : /// Serializes a value.
34 : /// </summary>
35 : /// <param name="context">The serialization context.</param><param name="args">The serialization args.</param><param name="value">The value.</param>
36 1 : public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Type value)
37 : {
38 : if (value == null)
39 : {
40 : context.Writer.WriteNull();
41 : }
42 : else
43 : {
44 : context.Writer.WriteString((value).AssemblyQualifiedName);
45 : }
46 : }
47 :
48 : /// <summary>
49 : /// Deserializes a value.
50 : /// </summary>
51 : /// <param name="context">The deserialization context.</param>
52 : /// <param name="args">The deserialization args.</param>
53 : /// <returns>
54 : /// A deserialized value.
55 : /// </returns>
56 : Type IBsonSerializer<Type>.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
57 : {
58 : var bsonReader = context.Reader;
59 : BsonType currentBsonType = bsonReader.CurrentBsonType;
60 : if (currentBsonType == BsonType.Null)
61 : return null;
62 :
63 : string assemblyQualifiedName;
64 : switch (currentBsonType)
65 : {
66 : case BsonType.Document:
67 : bsonReader.ReadStartDocument();
68 : assemblyQualifiedName = context.Reader.ReadString();
69 : // This moves the pointer forward.
70 : bsonReader.ReadBsonType();
71 : bsonReader.ReadEndDocument();
72 : break;
73 : default:
74 : assemblyQualifiedName = bsonReader.ReadString();
75 : break;
76 : }
77 : return Type.GetType(assemblyQualifiedName);
78 : }
79 :
80 : /// <summary>
81 : /// Serializes a value.
82 : /// </summary>
83 : /// <param name="context">The serialization context.</param><param name="args">The serialization args.</param><param name="value">The value.</param>
84 1 : public void Serialize(BsonSerializationContext context, BsonSerializationArgs args, object value)
85 : {
86 : Serialize(context, args, (Type)value);
87 : }
88 :
89 : /// <summary>
90 : /// Gets the type of the value.
91 : /// </summary>
92 : public Type ValueType { get; protected set; }
93 :
94 : #endregion
95 : }
96 : }
|