-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
BsonSerializerRegistry.cs
196 lines (179 loc) · 7.66 KB
/
BsonSerializerRegistry.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
/* 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.Collections.Concurrent;
using System.Reflection;
namespace MongoDB.Bson.Serialization
{
/// <summary>
/// Default, global implementation of an <see cref="IBsonSerializerRegistry"/>.
/// </summary>
public sealed class BsonSerializerRegistry : IBsonSerializerRegistry
{
// private fields
private readonly ConcurrentDictionary<Type, IBsonSerializer> _cache;
private readonly ConcurrentStack<IBsonSerializationProvider> _serializationProviders;
private readonly Func<Type, IBsonSerializer> _createSerializer;
// constructors
/// <summary>
/// Initializes a new instance of the <see cref="BsonSerializerRegistry"/> class.
/// </summary>
public BsonSerializerRegistry()
{
_cache = new ConcurrentDictionary<Type, IBsonSerializer>();
_serializationProviders = new ConcurrentStack<IBsonSerializationProvider>();
_createSerializer = CreateSerializer;
}
// public methods
/// <summary>
/// Gets the serializer for the specified <paramref name="type" />.
/// If none is already registered, the serialization providers will be used to create a serializer and it will be automatically registered.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>
/// The serializer.
/// </returns>
public IBsonSerializer GetSerializer(Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
var typeInfo = type.GetTypeInfo();
if (typeInfo.IsGenericType && typeInfo.ContainsGenericParameters)
{
var message = string.Format("Generic type {0} has unassigned type parameters.", BsonUtils.GetFriendlyTypeName(type));
throw new ArgumentException(message, "type");
}
return _cache.GetOrAdd(type, _createSerializer);
}
/// <summary>
/// Gets the serializer for the specified <typeparamref name="T" />.
/// If none is already registered, the serialization providers will be used to create a serializer and it will be automatically registered.
/// </summary>
/// <typeparam name="T">The value type of the serializer.</typeparam>
/// <returns>
/// The serializer.
/// </returns>
public IBsonSerializer<T> GetSerializer<T>()
{
return (IBsonSerializer<T>)GetSerializer(typeof(T));
}
/// <summary>
/// Registers the serializer.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="serializer">The serializer.</param>
public void RegisterSerializer(Type type, IBsonSerializer serializer)
{
if (type == null)
{
throw new ArgumentNullException("type");
}
if (serializer == null)
{
throw new ArgumentNullException("serializer");
}
EnsureRegisteringASerializerForThisTypeIsAllowed(type);
if (!_cache.TryAdd(type, serializer))
{
var message = string.Format("There is already a serializer registered for type {0}.", BsonUtils.GetFriendlyTypeName(type));
throw new BsonSerializationException(message);
}
}
/// <summary>
/// Registers the serialization provider. This behaves like a stack, so the
/// last provider registered is the first provider consulted.
/// </summary>
/// <param name="serializationProvider">The serialization provider.</param>
public void RegisterSerializationProvider(IBsonSerializationProvider serializationProvider)
{
if (serializationProvider == null)
{
throw new ArgumentNullException("serializationProvider");
}
_serializationProviders.Push(serializationProvider);
}
/// <summary>
/// Tries to register the serializer.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="serializer">The serializer.</param>
/// <returns>True if the serializer was registered on this call, false if the same serializer was already registered on a previous call, throws an exception if a different serializer was already registered.</returns>
public bool TryRegisterSerializer(Type type, IBsonSerializer serializer)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
if (serializer == null)
{
throw new ArgumentNullException(nameof(serializer));
}
EnsureRegisteringASerializerForThisTypeIsAllowed(type);
if (_cache.TryAdd(type, serializer))
{
return true;
}
else
{
var existingSerializer = _cache[type];
if (!existingSerializer.Equals(serializer))
{
var message = $"There is already a different serializer registered for type {BsonUtils.GetFriendlyTypeName(type)}.";
throw new BsonSerializationException(message);
}
return false;
}
}
// private methods
private IBsonSerializer CreateSerializer(Type type)
{
foreach (var serializationProvider in _serializationProviders)
{
IBsonSerializer serializer;
var registryAwareSerializationProvider = serializationProvider as IRegistryAwareBsonSerializationProvider;
if (registryAwareSerializationProvider != null)
{
serializer = registryAwareSerializationProvider.GetSerializer(type, this);
}
else
{
serializer = serializationProvider.GetSerializer(type);
}
if (serializer != null)
{
return serializer;
}
}
var message = string.Format("No serializer found for type {0}.", type.FullName);
throw new BsonSerializationException(message);
}
private void EnsureRegisteringASerializerForThisTypeIsAllowed(Type type)
{
var typeInfo = type.GetTypeInfo();
if (typeof(BsonValue).GetTypeInfo().IsAssignableFrom(type))
{
var message = string.Format("A serializer cannot be registered for type {0} because it is a subclass of BsonValue.", BsonUtils.GetFriendlyTypeName(type));
throw new BsonSerializationException(message);
}
if (typeInfo.IsGenericType && typeInfo.ContainsGenericParameters)
{
var message = string.Format("Generic type {0} has unassigned type parameters.", BsonUtils.GetFriendlyTypeName(type));
throw new ArgumentException(message, "type");
}
}
}
}