| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737 |
- /* Copyright 2010-2016 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.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;
- using MongoDB.Bson.Serialization.Serializers;
- 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;
- private static HashSet<Type> __typesWithRegisteredKnownTypes = new HashSet<Type>();
- 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;
- if (__discriminators.TryGetValue(discriminator, out hashSet))
- {
- foreach (var type in hashSet)
- {
- if (nominalType.GetTypeInfo().IsAssignableFrom(type))
- {
- if (actualType == null)
- {
- actualType = type;
- }
- else
- {
- string message = string.Format("Ambiguous discriminator '{0}'.", discriminator);
- throw new BsonSerializationException(message);
- }
- }
- }
- }
- if (actualType == null && 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 (!nominalType.GetTypeInfo().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)
- {
- var typeInfo = type.GetTypeInfo();
- __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))
- {
- 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)
- {
- // TODO: should convention for interfaces be inherited from parent interfaces?
- convention = LookupDiscriminatorConvention(typeof(object));
- 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 = type.GetTypeInfo().BaseType;
- while (convention == null)
- {
- 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();
- }
- }
- /// <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)
- {
- var typeInfo = type.GetTypeInfo();
- __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))
- {
- 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 = type.GetTypeInfo().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))
- {
- 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))
- {
- var serializer = LookupSerializer(nominalType);
- var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator);
- serializer.Serialize(context, args, value);
- }
- // internal static methods
- internal static void EnsureKnownTypesAreRegistered(Type nominalType)
- {
- __configLock.EnterReadLock();
- try
- {
- if (__typesWithRegisteredKnownTypes.Contains(nominalType))
- {
- return;
- }
- }
- finally
- {
- __configLock.ExitReadLock();
- }
- __configLock.EnterWriteLock();
- try
- {
- if (!__typesWithRegisteredKnownTypes.Contains(nominalType))
- {
- // only call LookupClassMap for classes with a BsonKnownTypesAttribute
- #if NET45
- var knownTypesAttribute = nominalType.GetTypeInfo().GetCustomAttributes(typeof(BsonKnownTypesAttribute), false);
- #else
- var knownTypesAttribute = nominalType.GetTypeInfo().GetCustomAttributes(typeof(BsonKnownTypesAttribute), false).ToArray();
- #endif
- if (knownTypesAttribute != null && knownTypesAttribute.Length > 0)
- {
- // try and force a scan of the known types
- LookupSerializer(nominalType);
- }
- __typesWithRegisteredKnownTypes.Add(nominalType);
- }
- }
- 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);
- }
- }
- }
|