/// <inheritdoc />
        protected sealed override TypeToRegister BuildTypeToRegisterForPostInitializationRegistration(
            Type type,
            Type recursiveOriginType,
            Type directOriginType,
            MemberTypesToInclude memberTypesToInclude,
            RelatedTypesToInclude relatedTypesToInclude)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (recursiveOriginType == null)
            {
                throw new ArgumentNullException(nameof(recursiveOriginType));
            }

            if (directOriginType == null)
            {
                throw new ArgumentNullException(nameof(directOriginType));
            }

            var genericTypeDefinition = type.GetGenericTypeDefinition();

            var genericTypeDefinitionTypeToRegister = (TypeToRegisterForBson)this.RegisteredTypeToRegistrationDetailsMap[genericTypeDefinition].TypeToRegister;

            var result = new TypeToRegisterForBson(type, recursiveOriginType, directOriginType, memberTypesToInclude, relatedTypesToInclude, genericTypeDefinitionTypeToRegister.BsonSerializerBuilder, genericTypeDefinitionTypeToRegister.PropertyNameWhitelist);

            return(result);
        }
        /// <inheritdoc />
        public override TypeToRegister CreateSpawnedTypeToRegister(
            Type type,
            TypeToIncludeOrigin typeToIncludeOrigin)
        {
            var result = new TypeToRegisterForBson(type, this.RecursiveOriginType, this.Type, this.MemberTypesToInclude, this.RelatedTypesToInclude, this.BsonSerializerBuilder, this.PropertyNameWhitelist);

            return(result);
        }
Example #3
0
        private void ProcessTypeToRegisterForBson(
            TypeToRegisterForBson typeToRegisterForBson,
            SerializationConfigurationType registeringSerializationConfigurationType)
        {
            var type = typeToRegisterForBson.Type;

            var bsonSerializerBuilder = typeToRegisterForBson.BsonSerializerBuilder;

            var propertyNameWhitelist = typeToRegisterForBson.PropertyNameWhitelist;

            if (registeringSerializationConfigurationType == this.SerializationConfigurationType)
            {
                if (bsonSerializerBuilder == null)
                {
                    try
                    {
                        if (type.IsClass)
                        {
                            var bsonClassMap = AutomaticallyBuildBsonClassMap(type, propertyNameWhitelist);

                            BsonClassMap.RegisterClassMap(bsonClassMap);
                        }

                        // we are not 100% sure whether interface types or abstract types need to be registered
                        // but there doesn't seem to be any harm in doing so.
                        BsonSerializer.RegisterDiscriminatorConvention(type, ObcBsonDiscriminatorConvention.Instance);
                    }
                    catch (Exception ex)
                    {
                        throw new BsonSerializationConfigurationException(Invariant($"Failed to run {nameof(BsonClassMap.RegisterClassMap)} on {type.FullName}"), ex);
                    }
                }
                else
                {
                    // see comments in RegisteredTypeBsonSerializer<T> for why we are wrapping the serializer
                    var registeredTypeBsonSerializer = RegisteredTypeBsonSerializer.Build(type, bsonSerializerBuilder.BsonSerializerBuilderFunc());
                    BsonSerializer.RegisterSerializer(type, registeredTypeBsonSerializer);
                }
            }

            if (bsonSerializerBuilder != null)
            {
                this.typesWithCustomSerializerOrPropertyNamesWhitelist.Add(type, null);

                if (bsonSerializerBuilder.OutputKind == BsonSerializerOutputKind.String)
                {
                    this.typesWithCustomStringSerializers.Add(type, null);
                }
            }

            if (propertyNameWhitelist != null)
            {
                this.typesWithCustomSerializerOrPropertyNamesWhitelist.Add(type, null);
            }
        }
Example #4
0
        /// <summary>
        /// Builds a <see cref="TypeToRegisterForBson"/> from a type using the most sensible settings.
        /// </summary>
        /// <param name="type">The type to register.</param>
        /// <returns>
        /// The type to register for BSON serialization.
        /// </returns>
        public static TypeToRegisterForBson ToTypeToRegisterForBson(
            this Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            var result = new TypeToRegisterForBson(type, MemberTypesToInclude.All, RelatedTypesToInclude.Default, null, null);

            return(result);
        }
Example #5
0
        /// <summary>
        /// Builds a <see cref="TypeToRegisterForBson"/> for a type using the most sensible settings,
        /// with a specified <see cref="IStringSerializeAndDeserialize"/> to use everywhere the type appears.
        /// </summary>
        /// <param name="type">The type to register.</param>
        /// <param name="stringSerializer">The string serializer to use for <paramref name="type"/>.</param>
        /// <returns>
        /// The type to register for BSON serialization.
        /// </returns>
        public static TypeToRegisterForBson ToTypeToRegisterForBsonUsingStringSerializer(
            this Type type,
            IStringSerializeAndDeserialize stringSerializer)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (stringSerializer == null)
            {
                throw new ArgumentNullException(nameof(stringSerializer));
            }

            var serializer = StringSerializerBackedBsonSerializer.Build(type, stringSerializer);

            IBsonSerializer BsonSerializerBuilderFunc() => serializer;

            var bsonSerializerBuilder = new BsonSerializerBuilder(BsonSerializerBuilderFunc, BsonSerializerOutputKind.String);

            var result = new TypeToRegisterForBson(type, MemberTypesToInclude.None, RelatedTypesToInclude.Default, bsonSerializerBuilder, null);

            return(result);
        }