-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathDiscriminatedInterfaceSerializer.cs
174 lines (155 loc) · 7.01 KB
/
DiscriminatedInterfaceSerializer.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
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Reflection;
using MongoDB.Bson.Serialization.Conventions;
namespace MongoDB.Bson.Serialization.Serializers
{
/// <summary>
/// An interface implemented by DiscriminatedInterfaceSerializer.
/// </summary>
public interface IDiscriminatedInterfaceSerializer
{
/// <summary>
/// Gets the interface serializer.
/// </summary>
IBsonSerializer InterfaceSerializer { get; }
}
/// <summary>
/// Represents a serializer for Interfaces.
/// </summary>
/// <typeparam name="TInterface">The type of the interface.</typeparam>
public class DiscriminatedInterfaceSerializer<TInterface> :
SerializerBase<TInterface>,
IBsonDocumentSerializer,
IDiscriminatedInterfaceSerializer
// where TInterface is an interface
{
#region static
private static IBsonSerializer<TInterface> CreateInterfaceSerializer()
{
var classMapDefinition = typeof(BsonClassMap<>);
var classMapType = classMapDefinition.MakeGenericType(typeof(TInterface));
var classMap = (BsonClassMap)Activator.CreateInstance(classMapType);
classMap.AutoMap();
classMap.Freeze();
return new BsonClassMapSerializer<TInterface>(classMap);
}
#endregion
// private fields
private readonly Type _interfaceType;
private readonly IDiscriminatorConvention _discriminatorConvention;
private readonly IBsonSerializer<TInterface> _interfaceSerializer;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="DiscriminatedInterfaceSerializer{TInterface}" /> class.
/// </summary>
public DiscriminatedInterfaceSerializer()
: this(discriminatorConvention: null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DiscriminatedInterfaceSerializer{TInterface}" /> class.
/// </summary>
/// <param name="discriminatorConvention">The discriminator convention.</param>
/// <exception cref="System.ArgumentException">interfaceType</exception>
/// <exception cref="System.ArgumentNullException">interfaceType</exception>
public DiscriminatedInterfaceSerializer(IDiscriminatorConvention discriminatorConvention)
: this(discriminatorConvention, CreateInterfaceSerializer())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DiscriminatedInterfaceSerializer{TInterface}" /> class.
/// </summary>
/// <param name="discriminatorConvention">The discriminator convention.</param>
/// <param name="interfaceSerializer">The interface serializer (necessary to support LINQ queries).</param>
/// <exception cref="System.ArgumentException">interfaceType</exception>
/// <exception cref="System.ArgumentNullException">interfaceType</exception>
public DiscriminatedInterfaceSerializer(IDiscriminatorConvention discriminatorConvention, IBsonSerializer<TInterface> interfaceSerializer)
{
var interfaceTypeInfo = typeof(TInterface).GetTypeInfo();
if (!interfaceTypeInfo.IsInterface)
{
var message = string.Format("{0} is not an interface.", typeof(TInterface).FullName);
throw new ArgumentException(message, "<TInterface>");
}
_interfaceType = typeof(TInterface);
_discriminatorConvention = discriminatorConvention ?? BsonSerializer.LookupDiscriminatorConvention(typeof(TInterface));
_interfaceSerializer = interfaceSerializer;
}
// public properties
/// <summary>
/// Gets the interface serializer.
/// </summary>
public IBsonSerializer<TInterface> InterfaceSerializer => _interfaceSerializer;
IBsonSerializer IDiscriminatedInterfaceSerializer.InterfaceSerializer => _interfaceSerializer;
// public methods
/// <summary>
/// Deserializes a value.
/// </summary>
/// <param name="context">The deserialization context.</param>
/// <param name="args">The deserialization args.</param>
/// <returns>A deserialized value.</returns>
/// <exception cref="System.FormatException"></exception>
public override TInterface Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
{
var bsonReader = context.Reader;
if (bsonReader.GetCurrentBsonType() == BsonType.Null)
{
bsonReader.ReadNull();
return default(TInterface);
}
else
{
var actualType = _discriminatorConvention.GetActualType(bsonReader, typeof(TInterface));
if (actualType == _interfaceType)
{
var message = string.Format("Unable to determine actual type of object to deserialize for interface type {0}.", _interfaceType.FullName);
throw new FormatException(message);
}
var serializer = BsonSerializer.LookupSerializer(actualType);
return (TInterface)serializer.Deserialize(context, args);
}
}
/// <summary>
/// Serializes a value.
/// </summary>
/// <param name="context">The serialization context.</param>
/// <param name="args">The serialization args.</param>
/// <param name="value">The document.</param>
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, TInterface value)
{
var bsonWriter = context.Writer;
if (value == null)
{
bsonWriter.WriteNull();
return;
}
args.DiscriminatorConvention = _discriminatorConvention;
var serializer = BsonSerializer.LookupSerializer(value.GetType());
serializer.Serialize(context, args, value);
}
/// <inheritdoc/>
public bool TryGetMemberSerializationInfo(string memberName, out BsonSerializationInfo serializationInfo)
{
if (_interfaceSerializer is IBsonDocumentSerializer documentSerializer)
{
return documentSerializer.TryGetMemberSerializationInfo(memberName, out serializationInfo);
}
serializationInfo = null;
return false;
}
}
}