Line data Source code
1 : using System;
2 : using MongoDB.Bson;
3 : using MongoDB.Bson.IO;
4 : using MongoDB.Bson.Serialization;
5 :
6 : namespace Cqrs.Mongo.Serialisers
7 : {
8 : public class TypeSerialiser : IBsonSerializer
9 0 : {
10 0 : public object Deserialize(BsonReader reader, Type nominalType, IBsonSerializationOptions options)
11 : {
12 : var actualType = nominalType;
13 : return Deserialize(reader, nominalType, actualType, options);
14 : }
15 :
16 0 : public object Deserialize(BsonReader reader, Type nominalType, Type actualType, IBsonSerializationOptions options)
17 : {
18 : if (reader.CurrentBsonType == BsonType.Null)
19 : {
20 : return null;
21 : }
22 : string assemblyQualifiedName = reader.ReadString();
23 : return Type.GetType(assemblyQualifiedName);
24 : }
25 :
26 : /// <summary>
27 : /// Gets the default serialization options for this serializer.
28 : /// </summary>
29 : /// <returns>
30 : /// The default serialization options for this serializer.
31 : /// </returns>
32 1 : public IBsonSerializationOptions GetDefaultSerializationOptions()
33 : {
34 : return null;
35 : }
36 :
37 0 : public void Serialize(BsonWriter writer, Type nominalType, object value, IBsonSerializationOptions options)
38 : {
39 : if (value == null)
40 : {
41 : writer.WriteNull();
42 : }
43 : else
44 : {
45 : writer.WriteString(((Type)value).AssemblyQualifiedName);
46 : }
47 : }
48 : }
49 : }
|