-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathBsonSerializer.cs
764 lines (702 loc) · 31.9 KB
/
BsonSerializer.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/* 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.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
// don't add using statement for MongoDB.Bson.Serialization.Serializers to minimize dependencies on DefaultSerializer
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Bson.Serialization.IdGenerators;
namespace MongoDB.Bson.Serialization
{
/// <summary>
/// A static class that represents the BSON serialization functionality.
/// </summary>
public static class BsonSerializer
{
// private static fields
private static ReaderWriterLockSlim __configLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
private static Dictionary<Type, IIdGenerator> __idGenerators = new Dictionary<Type, IIdGenerator>();
private static Dictionary<Type, IDiscriminatorConvention> __discriminatorConventions = new Dictionary<Type, IDiscriminatorConvention>();
private static Dictionary<BsonValue, HashSet<Type>> __discriminators = new Dictionary<BsonValue, HashSet<Type>>();
private static HashSet<Type> __discriminatedTypes = new HashSet<Type>();
private static BsonSerializerRegistry __serializerRegistry;
private static TypeMappingSerializationProvider __typeMappingSerializationProvider;
// ConcurrentDictionary<Type, object> is being used as a concurrent set of Type. The values will always be null.
private static ConcurrentDictionary<Type, object> __typesWithRegisteredKnownTypes = new ConcurrentDictionary<Type, object>();
private static bool __useNullIdChecker = false;
private static bool __useZeroIdChecker = false;
// static constructor
static BsonSerializer()
{
CreateSerializerRegistry();
RegisterIdGenerators();
}
// public static properties
/// <summary>
/// Gets the serializer registry.
/// </summary>
public static IBsonSerializerRegistry SerializerRegistry
{
get { return __serializerRegistry; }
}
/// <summary>
/// Gets or sets whether to use the NullIdChecker on reference Id types that don't have an IdGenerator registered.
/// </summary>
public static bool UseNullIdChecker
{
get { return __useNullIdChecker; }
set { __useNullIdChecker = value; }
}
/// <summary>
/// Gets or sets whether to use the ZeroIdChecker on value Id types that don't have an IdGenerator registered.
/// </summary>
public static bool UseZeroIdChecker
{
get { return __useZeroIdChecker; }
set { __useZeroIdChecker = value; }
}
// internal static properties
internal static ReaderWriterLockSlim ConfigLock
{
get { return __configLock; }
}
// public static methods
/// <summary>
/// Deserializes an object from a BsonDocument.
/// </summary>
/// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
/// <param name="document">The BsonDocument.</param>
/// <param name="configurator">The configurator.</param>
/// <returns>A deserialized value.</returns>
public static TNominalType Deserialize<TNominalType>(BsonDocument document, Action<BsonDeserializationContext.Builder> configurator = null)
{
using (var bsonReader = new BsonDocumentReader(document))
{
return Deserialize<TNominalType>(bsonReader, configurator);
}
}
/// <summary>
/// Deserializes a value.
/// </summary>
/// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="configurator">The configurator.</param>
/// <returns>A deserialized value.</returns>
public static TNominalType Deserialize<TNominalType>(IBsonReader bsonReader, Action<BsonDeserializationContext.Builder> configurator = null)
{
var serializer = LookupSerializer<TNominalType>();
var context = BsonDeserializationContext.CreateRoot(bsonReader, configurator);
return serializer.Deserialize(context);
}
/// <summary>
/// Deserializes an object from a BSON byte array.
/// </summary>
/// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
/// <param name="bytes">The BSON byte array.</param>
/// <param name="configurator">The configurator.</param>
/// <returns>A deserialized value.</returns>
public static TNominalType Deserialize<TNominalType>(byte[] bytes, Action<BsonDeserializationContext.Builder> configurator = null)
{
using (var buffer = new ByteArrayBuffer(bytes, isReadOnly: true))
using (var stream = new ByteBufferStream(buffer))
{
return Deserialize<TNominalType>(stream, configurator);
}
}
/// <summary>
/// Deserializes an object from a BSON Stream.
/// </summary>
/// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
/// <param name="stream">The BSON Stream.</param>
/// <param name="configurator">The configurator.</param>
/// <returns>A deserialized value.</returns>
public static TNominalType Deserialize<TNominalType>(Stream stream, Action<BsonDeserializationContext.Builder> configurator = null)
{
using (var bsonReader = new BsonBinaryReader(stream))
{
return Deserialize<TNominalType>(bsonReader, configurator);
}
}
/// <summary>
/// Deserializes an object from a JSON string.
/// </summary>
/// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
/// <param name="json">The JSON string.</param>
/// <param name="configurator">The configurator.</param>
/// <returns>A deserialized value.</returns>
public static TNominalType Deserialize<TNominalType>(string json, Action<BsonDeserializationContext.Builder> configurator = null)
{
using (var bsonReader = new JsonReader(json))
{
return Deserialize<TNominalType>(bsonReader, configurator);
}
}
/// <summary>
/// Deserializes an object from a JSON TextReader.
/// </summary>
/// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
/// <param name="textReader">The JSON TextReader.</param>
/// <param name="configurator">The configurator.</param>
/// <returns>A deserialized value.</returns>
public static TNominalType Deserialize<TNominalType>(TextReader textReader, Action<BsonDeserializationContext.Builder> configurator = null)
{
using (var bsonReader = new JsonReader(textReader))
{
return Deserialize<TNominalType>(bsonReader, configurator);
}
}
/// <summary>
/// Deserializes an object from a BsonDocument.
/// </summary>
/// <param name="document">The BsonDocument.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="configurator">The configurator.</param>
/// <returns>A deserialized value.</returns>
public static object Deserialize(BsonDocument document, Type nominalType, Action<BsonDeserializationContext.Builder> configurator = null)
{
using (var bsonReader = new BsonDocumentReader(document))
{
return Deserialize(bsonReader, nominalType, configurator);
}
}
/// <summary>
/// Deserializes a value.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="configurator">The configurator.</param>
/// <returns>A deserialized value.</returns>
public static object Deserialize(IBsonReader bsonReader, Type nominalType, Action<BsonDeserializationContext.Builder> configurator = null)
{
var serializer = LookupSerializer(nominalType);
var context = BsonDeserializationContext.CreateRoot(bsonReader, configurator);
return serializer.Deserialize(context);
}
/// <summary>
/// Deserializes an object from a BSON byte array.
/// </summary>
/// <param name="bytes">The BSON byte array.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="configurator">The configurator.</param>
/// <returns>A deserialized value.</returns>
public static object Deserialize(byte[] bytes, Type nominalType, Action<BsonDeserializationContext.Builder> configurator = null)
{
using (var buffer = new ByteArrayBuffer(bytes, isReadOnly: true))
using (var stream = new ByteBufferStream(buffer))
{
return Deserialize(stream, nominalType, configurator);
}
}
/// <summary>
/// Deserializes an object from a BSON Stream.
/// </summary>
/// <param name="stream">The BSON Stream.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="configurator">The configurator.</param>
/// <returns>A deserialized value.</returns>
public static object Deserialize(Stream stream, Type nominalType, Action<BsonDeserializationContext.Builder> configurator = null)
{
using (var bsonReader = new BsonBinaryReader(stream))
{
return Deserialize(bsonReader, nominalType, configurator);
}
}
/// <summary>
/// Deserializes an object from a JSON string.
/// </summary>
/// <param name="json">The JSON string.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="configurator">The configurator.</param>
/// <returns>A deserialized value.</returns>
public static object Deserialize(string json, Type nominalType, Action<BsonDeserializationContext.Builder> configurator = null)
{
using (var bsonReader = new JsonReader(json))
{
return Deserialize(bsonReader, nominalType, configurator);
}
}
/// <summary>
/// Deserializes an object from a JSON TextReader.
/// </summary>
/// <param name="textReader">The JSON TextReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="configurator">The configurator.</param>
/// <returns>A deserialized value.</returns>
public static object Deserialize(TextReader textReader, Type nominalType, Action<BsonDeserializationContext.Builder> configurator = null)
{
using (var bsonReader = new JsonReader(textReader))
{
return Deserialize(bsonReader, nominalType, configurator);
}
}
/// <summary>
/// Returns whether the given type has any discriminators registered for any of its subclasses.
/// </summary>
/// <param name="type">A Type.</param>
/// <returns>True if the type is discriminated.</returns>
public static bool IsTypeDiscriminated(Type type)
{
var typeInfo = type.GetTypeInfo();
return typeInfo.IsInterface || __discriminatedTypes.Contains(type);
}
/// <summary>
/// Looks up the actual type of an object to be deserialized.
/// </summary>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="discriminator">The discriminator.</param>
/// <returns>The actual type of the object.</returns>
public static Type LookupActualType(Type nominalType, BsonValue discriminator)
{
if (discriminator == null)
{
return nominalType;
}
// note: EnsureKnownTypesAreRegistered handles its own locking so call from outside any lock
EnsureKnownTypesAreRegistered(nominalType);
__configLock.EnterReadLock();
try
{
Type actualType = null;
HashSet<Type> hashSet;
var nominalTypeInfo = nominalType.GetTypeInfo();
if (__discriminators.TryGetValue(discriminator, out hashSet))
{
foreach (var type in hashSet)
{
if (nominalTypeInfo.IsAssignableFrom(type))
{
if (actualType == null)
{
actualType = type;
}
else
{
string message = string.Format("Ambiguous discriminator '{0}'.", discriminator);
throw new BsonSerializationException(message);
}
}
}
// no need for additional checks, we found the right type
if (actualType != null)
{
return actualType;
}
}
if (discriminator.IsString)
{
actualType = TypeNameDiscriminator.GetActualType(discriminator.AsString); // see if it's a Type name
}
if (actualType == null)
{
string message = string.Format("Unknown discriminator value '{0}'.", discriminator);
throw new BsonSerializationException(message);
}
if (!nominalTypeInfo.IsAssignableFrom(actualType))
{
string message = string.Format(
"Actual type {0} is not assignable to expected type {1}.",
actualType.FullName, nominalType.FullName);
throw new BsonSerializationException(message);
}
return actualType;
}
finally
{
__configLock.ExitReadLock();
}
}
/// <summary>
/// Looks up the discriminator convention for a type.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>A discriminator convention.</returns>
public static IDiscriminatorConvention LookupDiscriminatorConvention(Type type)
{
__configLock.EnterReadLock();
try
{
IDiscriminatorConvention convention;
if (__discriminatorConventions.TryGetValue(type, out convention))
{
return convention;
}
}
finally
{
__configLock.ExitReadLock();
}
__configLock.EnterWriteLock();
try
{
IDiscriminatorConvention convention;
if (!__discriminatorConventions.TryGetValue(type, out convention))
{
var typeInfo = type.GetTypeInfo();
if (type == typeof(object))
{
// if there is no convention registered for object register the default one
convention = new ObjectDiscriminatorConvention("_t");
RegisterDiscriminatorConvention(typeof(object), convention);
}
else if (typeInfo.IsInterface)
{
convention = CreateInterfaceDiscriminatorConvention(type);
RegisterDiscriminatorConvention(type, convention);
}
else
{
// inherit the discriminator convention from the closest parent (that isn't object) that has one
// otherwise default to the standard hierarchical convention
Type parentType = typeInfo.BaseType;
while (true)
{
if (parentType == typeof(object))
{
convention = StandardDiscriminatorConvention.Hierarchical;
break;
}
if (__discriminatorConventions.TryGetValue(parentType, out convention))
{
break;
}
parentType = parentType.GetTypeInfo().BaseType;
}
// register this convention for all types between this and the parent type where we found the convention
var unregisteredType = type;
while (unregisteredType != parentType)
{
RegisterDiscriminatorConvention(unregisteredType, convention);
unregisteredType = unregisteredType.GetTypeInfo().BaseType;
}
}
}
return convention;
}
finally
{
__configLock.ExitWriteLock();
}
}
private static IDiscriminatorConvention CreateInterfaceDiscriminatorConvention(Type type)
{
var discriminatorConventionType = typeof(InterfaceDiscriminatorConvention<>).MakeGenericType(type);
return (IDiscriminatorConvention) Activator.CreateInstance(discriminatorConventionType, "_t");
}
/// <summary>
/// Looks up an IdGenerator.
/// </summary>
/// <param name="type">The Id type.</param>
/// <returns>An IdGenerator for the Id type.</returns>
public static IIdGenerator LookupIdGenerator(Type type)
{
__configLock.EnterReadLock();
try
{
IIdGenerator idGenerator;
if (__idGenerators.TryGetValue(type, out idGenerator))
{
return idGenerator;
}
}
finally
{
__configLock.ExitReadLock();
}
__configLock.EnterWriteLock();
try
{
IIdGenerator idGenerator;
if (!__idGenerators.TryGetValue(type, out idGenerator))
{
var typeInfo = type.GetTypeInfo();
if (typeInfo.IsValueType && __useZeroIdChecker)
{
var iEquatableDefinition = typeof(IEquatable<>);
var iEquatableType = iEquatableDefinition.MakeGenericType(type);
if (iEquatableType.GetTypeInfo().IsAssignableFrom(type))
{
var zeroIdCheckerDefinition = typeof(ZeroIdChecker<>);
var zeroIdCheckerType = zeroIdCheckerDefinition.MakeGenericType(type);
idGenerator = (IIdGenerator)Activator.CreateInstance(zeroIdCheckerType);
}
}
else if (__useNullIdChecker)
{
idGenerator = NullIdChecker.Instance;
}
else
{
idGenerator = null;
}
__idGenerators[type] = idGenerator; // remember it even if it's null
}
return idGenerator;
}
finally
{
__configLock.ExitWriteLock();
}
}
/// <summary>
/// Looks up a serializer for a Type.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <returns>A serializer for type T.</returns>
public static IBsonSerializer<T> LookupSerializer<T>()
{
return (IBsonSerializer<T>)LookupSerializer(typeof(T));
}
/// <summary>
/// Looks up a serializer for a Type.
/// </summary>
/// <param name="type">The Type.</param>
/// <returns>A serializer for the Type.</returns>
public static IBsonSerializer LookupSerializer(Type type)
{
return __serializerRegistry.GetSerializer(type);
}
/// <summary>
/// Registers the discriminator for a type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="discriminator">The discriminator.</param>
public static void RegisterDiscriminator(Type type, BsonValue discriminator)
{
var typeInfo = type.GetTypeInfo();
if (typeInfo.IsInterface)
{
var message = string.Format("Discriminators can only be registered for classes, not for interface {0}.", type.FullName);
throw new BsonSerializationException(message);
}
__configLock.EnterWriteLock();
try
{
HashSet<Type> hashSet;
if (!__discriminators.TryGetValue(discriminator, out hashSet))
{
hashSet = new HashSet<Type>();
__discriminators.Add(discriminator, hashSet);
}
if (!hashSet.Contains(type))
{
hashSet.Add(type);
// mark all base types as discriminated (so we know that it's worth reading a discriminator)
for (var baseType = typeInfo.BaseType; baseType != null; baseType = baseType.GetTypeInfo().BaseType)
{
__discriminatedTypes.Add(baseType);
}
}
}
finally
{
__configLock.ExitWriteLock();
}
}
/// <summary>
/// Registers the discriminator convention for a type.
/// </summary>
/// <param name="type">Type type.</param>
/// <param name="convention">The discriminator convention.</param>
public static void RegisterDiscriminatorConvention(Type type, IDiscriminatorConvention convention)
{
__configLock.EnterWriteLock();
try
{
if (!__discriminatorConventions.ContainsKey(type))
{
__discriminatorConventions.Add(type, convention);
}
else
{
var message = string.Format("There is already a discriminator convention registered for type {0}.", type.FullName);
throw new BsonSerializationException(message);
}
}
finally
{
__configLock.ExitWriteLock();
}
}
/// <summary>
/// Registers a generic serializer definition for a generic type.
/// </summary>
/// <param name="genericTypeDefinition">The generic type.</param>
/// <param name="genericSerializerDefinition">The generic serializer definition.</param>
public static void RegisterGenericSerializerDefinition(
Type genericTypeDefinition,
Type genericSerializerDefinition)
{
__typeMappingSerializationProvider.RegisterMapping(genericTypeDefinition, genericSerializerDefinition);
}
/// <summary>
/// Registers an IdGenerator for an Id Type.
/// </summary>
/// <param name="type">The Id Type.</param>
/// <param name="idGenerator">The IdGenerator for the Id Type.</param>
public static void RegisterIdGenerator(Type type, IIdGenerator idGenerator)
{
__configLock.EnterWriteLock();
try
{
__idGenerators[type] = idGenerator;
}
finally
{
__configLock.ExitWriteLock();
}
}
/// <summary>
/// Registers a serialization provider.
/// </summary>
/// <param name="provider">The serialization provider.</param>
public static void RegisterSerializationProvider(IBsonSerializationProvider provider)
{
__serializerRegistry.RegisterSerializationProvider(provider);
}
/// <summary>
/// Registers a serializer for a type.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <param name="serializer">The serializer.</param>
public static void RegisterSerializer<T>(IBsonSerializer<T> serializer)
{
RegisterSerializer(typeof(T), serializer);
}
/// <summary>
/// Registers a serializer for a type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="serializer">The serializer.</param>
public static void RegisterSerializer(Type type, IBsonSerializer serializer)
{
__serializerRegistry.RegisterSerializer(type, serializer);
}
/// <summary>
/// Serializes a value.
/// </summary>
/// <typeparam name="TNominalType">The nominal type of the object.</typeparam>
/// <param name="bsonWriter">The BsonWriter.</param>
/// <param name="value">The object.</param>
/// <param name="configurator">The serialization context configurator.</param>
/// <param name="args">The serialization args.</param>
public static void Serialize<TNominalType>(
IBsonWriter bsonWriter,
TNominalType value,
Action<BsonSerializationContext.Builder> configurator = null,
BsonSerializationArgs args = default(BsonSerializationArgs))
{
args.SetOrValidateNominalType(typeof(TNominalType), "<TNominalType>");
var serializer = LookupSerializer<TNominalType>();
var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator);
serializer.Serialize(context, args, value);
}
/// <summary>
/// Serializes a value.
/// </summary>
/// <param name="bsonWriter">The BsonWriter.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="value">The object.</param>
/// <param name="configurator">The serialization context configurator.</param>
/// <param name="args">The serialization args.</param>
public static void Serialize(
IBsonWriter bsonWriter,
Type nominalType,
object value,
Action<BsonSerializationContext.Builder> configurator = null,
BsonSerializationArgs args = default(BsonSerializationArgs))
{
args.SetOrValidateNominalType(nominalType, "nominalType");
var serializer = LookupSerializer(nominalType);
var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator);
serializer.Serialize(context, args, value);
}
/// <summary>
/// Tries to register a serializer for a type.
/// </summary>
/// <param name="serializer">The serializer.</param>
/// <param name="type">The type.</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 static bool TryRegisterSerializer(Type type, IBsonSerializer serializer)
{
return __serializerRegistry.TryRegisterSerializer(type, serializer);
}
/// <summary>
/// Tries to register a serializer for a type.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <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 static bool TryRegisterSerializer<T>(IBsonSerializer<T> serializer)
{
return TryRegisterSerializer(typeof(T), serializer);
}
// internal static methods
internal static void EnsureKnownTypesAreRegistered(Type nominalType)
{
if (__typesWithRegisteredKnownTypes.ContainsKey(nominalType))
{
return;
}
__configLock.EnterWriteLock();
try
{
if (!__typesWithRegisteredKnownTypes.ContainsKey(nominalType))
{
// only call LookupClassMap for classes with a BsonKnownTypesAttribute
var hasKnownTypesAttribute = nominalType.GetTypeInfo().GetCustomAttributes(typeof(BsonKnownTypesAttribute), inherit: false).Any();
if (hasKnownTypesAttribute)
{
// try and force a scan of the known types
LookupSerializer(nominalType);
}
// NOTE: The nominalType MUST be added to __typesWithRegisteredKnownTypes after all registration
// work is done to ensure that other threads don't access a partially registered nominalType
// when performing the initial check above outside the __config lock.
__typesWithRegisteredKnownTypes[nominalType] = null;
}
}
finally
{
__configLock.ExitWriteLock();
}
}
// private static methods
private static void CreateSerializerRegistry()
{
__serializerRegistry = new BsonSerializerRegistry();
__typeMappingSerializationProvider = new TypeMappingSerializationProvider();
// order matters. It's in reverse order of how they'll get consumed
__serializerRegistry.RegisterSerializationProvider(new BsonClassMapSerializationProvider());
__serializerRegistry.RegisterSerializationProvider(new DiscriminatedInterfaceSerializationProvider());
__serializerRegistry.RegisterSerializationProvider(new CollectionsSerializationProvider());
__serializerRegistry.RegisterSerializationProvider(new PrimitiveSerializationProvider());
__serializerRegistry.RegisterSerializationProvider(new AttributedSerializationProvider());
__serializerRegistry.RegisterSerializationProvider(__typeMappingSerializationProvider);
__serializerRegistry.RegisterSerializationProvider(new BsonObjectModelSerializationProvider());
}
private static void RegisterIdGenerators()
{
RegisterIdGenerator(typeof(BsonObjectId), BsonObjectIdGenerator.Instance);
RegisterIdGenerator(typeof(Guid), GuidGenerator.Instance);
RegisterIdGenerator(typeof(ObjectId), ObjectIdGenerator.Instance);
}
}
}