-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathDiscriminatedInterfaceSerializer.cs
215 lines (194 loc) · 9.15 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
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
/* 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;
private readonly IBsonSerializer<object> _objectSerializer;
// 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(), objectSerializer: null)
{
}
/// <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)
: this(discriminatorConvention, interfaceSerializer, objectSerializer: null)
{
}
/// <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>
/// <param name="objectSerializer">The serializer that is used to serialize any objects.</param>
/// <exception cref="System.ArgumentException">interfaceType</exception>
/// <exception cref="System.ArgumentNullException">interfaceType</exception>
public DiscriminatedInterfaceSerializer(IDiscriminatorConvention discriminatorConvention, IBsonSerializer<TInterface> interfaceSerializer, IBsonSerializer<object> objectSerializer)
{
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));
_objectSerializer = objectSerializer ?? new ObjectSerializer(allowedTypes: type => typeof(TInterface).IsAssignableFrom(type));
if (_objectSerializer is ObjectSerializer standardObjectSerializer)
{
_objectSerializer = standardObjectSerializer.WithDiscriminatorConvention(_discriminatorConvention);
}
else
{
if (discriminatorConvention != null)
{
throw new BsonSerializationException("Can't set discriminator convention on custom object serializer.");
}
}
_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);
}
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
base.Equals(obj) &&
obj is DiscriminatedInterfaceSerializer<TInterface> other &&
object.Equals(_discriminatorConvention, other._discriminatorConvention) &&
object.Equals(_interfaceSerializer, other._interfaceSerializer);
}
/// <inheritdoc/>
public override int GetHashCode() => 0;
/// <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();
}
else
{
args.NominalType = typeof(object);
_objectSerializer.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;
}
}
}