public Type GetActualType(IBsonReader bsonReader, Type nominalType)
            {
                var bookmark = bsonReader.GetBookmark();

                bsonReader.ReadStartDocument();
                var actualType = nominalType;

                while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                {
                    var name = bsonReader.ReadName();
                    if (name == "OnlyInB")
                    {
                        actualType = typeof(B);
                        break;
                    }
                    else if (name == "OnlyInC")
                    {
                        actualType = typeof(C);
                        break;
                    }
                    bsonReader.SkipValue();
                }
                bsonReader.ReturnToBookmark(bookmark);
                return(actualType);
            }
        // public methods
        /// <summary>
        /// Gets the actual type of an object by reading the discriminator from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The reader.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <returns>The actual type.</returns>
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            // the BsonReader is sitting at the value whose actual type needs to be found
            var bsonType = bsonReader.GetCurrentBsonType();

            if (bsonType == BsonType.Document)
            {
                // ensure KnownTypes of nominalType are registered (so IsTypeDiscriminated returns correct answer)
                BsonSerializer.EnsureKnownTypesAreRegistered(nominalType);

                // we can skip looking for a discriminator if nominalType has no discriminated sub types
                if (BsonSerializer.IsTypeDiscriminated(nominalType))
                {
                    var bookmark = bsonReader.GetBookmark();
                    bsonReader.ReadStartDocument();
                    var actualType = nominalType;
                    if (bsonReader.FindElement(_elementName))
                    {
                        var context       = BsonDeserializationContext.CreateRoot <BsonValue>(bsonReader);
                        var discriminator = BsonValueSerializer.Instance.Deserialize(context);
                        if (discriminator.IsBsonArray)
                        {
                            discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator
                        }
                        actualType = BsonSerializer.LookupActualType(nominalType, discriminator);
                    }
                    bsonReader.ReturnToBookmark(bookmark);
                    return(actualType);
                }
            }

            return(nominalType);
        }
        internal static KeyValuePair <string, object> ReadDocument(this IBsonReader reader)
        {
            var key   = reader.ReadName();
            var value = default(object);

            // Setting bookmark to beginning of the document to rewind reader later
            var beginning = reader.GetBookmark();

            reader.ReadStartDocument();

            // Reading type before rewinding to the start
            var field = reader.ReadName();

            // Rewinding back to the start
            reader.ReturnToBookmark(beginning);

            switch (field)
            {
            case "_name":
                value = BsonSerializer.Deserialize <EntityReference>(reader);
                break;

            case "_money":
                value = BsonSerializer.Deserialize <Money>(reader);
                break;

            case "_option":
                value = BsonSerializer.Deserialize <OptionSetValue>(reader);
                break;
            }

            reader.ReadEndDocument();

            return(new KeyValuePair <string, object>(key, value));
        }
Beispiel #4
0
        /***************************************************/
        /**** Interface Methods                         ****/
        /***************************************************/

        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            // the BsonReader is sitting at the value whose actual type needs to be found
            var bsonType = bsonReader.GetCurrentBsonType();

            if (bsonType == BsonType.Document)
            {
                var bookmark = bsonReader.GetBookmark();
                bsonReader.ReadStartDocument();
                var actualType = nominalType;
                if (bsonReader.FindElement(ElementName))
                {
                    var context       = BsonDeserializationContext.CreateRoot(bsonReader);
                    var discriminator = BsonValueSerializer.Instance.Deserialize(context);
                    actualType = BsonSerializer.LookupActualType(nominalType, discriminator);
                }
                else
                {
                    actualType = typeof(CustomObject);
                }

                bsonReader.ReturnToBookmark(bookmark);
                return(actualType);
            }

            return(nominalType);
        }
    public Type GetActualType(IBsonReader bsonReader, Type nominalType)
    {
        if (!typeof(T).IsAssignableFrom(nominalType))
        {
            throw new Exception($"Cannot use DiscriminatorConvention<{typeof(T).Name}> for type " + nominalType);
        }

        var ret = nominalType;

        var bookmark = bsonReader.GetBookmark();

        bsonReader.ReadStartDocument();
        if (bsonReader.FindElement(ElementName))
        {
            var value = bsonReader.ReadString();
            ret = Type.GetType(value);

            if (ret == null)
            {
                throw new Exception("Could not find type from " + value);
            }

            if (!typeof(T).IsAssignableFrom(ret) && !ret.IsSubclassOf(typeof(T)))
            {
                throw new Exception("type is not an IRestriction");
            }
        }

        bsonReader.ReturnToBookmark(bookmark);

        return(ret);
    }
        // private methods
        private bool IsCSharpNullRepresentation(IBsonReader bsonReader)
        {
            var bookmark = bsonReader.GetBookmark();

            bsonReader.ReadStartDocument();
            var bsonType = bsonReader.ReadBsonType();

            if (bsonType == BsonType.Boolean)
            {
                var name = bsonReader.ReadName();
                if (name == "_csharpnull" || name == "$csharpnull")
                {
                    var value = bsonReader.ReadBoolean();
                    if (value)
                    {
                        bsonType = bsonReader.ReadBsonType();
                        if (bsonType == BsonType.EndOfDocument)
                        {
                            bsonReader.ReadEndDocument();
                            return(true);
                        }
                    }
                }
            }

            bsonReader.ReturnToBookmark(bookmark);
            return(false);
        }
Beispiel #7
0
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            if (nominalType == typeof(OrderModel))
            {
                var ret = nominalType;

                var bookmark = bsonReader.GetBookmark();
                bsonReader.ReadStartDocument();
                if (bsonReader.FindElement(orderType))
                {
                    var value = bsonReader.ReadString();

                    ret = OrderTypeResolver.ResolveOrderType(value);

                    if (ret == null)
                    {
                        throw new Exception("Could not find type " + value);
                    }

                    if (!ret.IsSubclassOf(typeof(OrderModel)))
                    {
                        throw new Exception("Database type does not inherit from OrderModel.");
                    }
                }

                bsonReader.ReturnToBookmark(bookmark);

                return(ret);
            }
            else
            {
                return(nominalType);
            }
        }
Beispiel #8
0
    /// <inheritdoc/>
    public Type GetActualType(IBsonReader bsonReader, Type nominalType)
    {
        ThrowIfNominalTypeIsIncorrect(nominalType);
        var bookmark = bsonReader.GetBookmark();

        bsonReader.ReadStartDocument();
        ObjectId id = default;

        while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
        {
            var fieldName = bsonReader.ReadName();
            if (fieldName == ElementName)
            {
                var partitioned = bsonReader.ReadBoolean();
                bsonReader.ReturnToBookmark(bookmark);
                return(partitioned ? typeof(Partitioned.PartitionedStreamProcessorState) : typeof(StreamProcessorState));
            }
            else if (fieldName == "_id")
            {
                id = bsonReader.ReadObjectId();
            }
            else
            {
                bsonReader.SkipValue();
            }
        }

        bsonReader.ReturnToBookmark(bookmark);
        throw new StreamProcessorStateDocumentIsMissingPartitionedField(id);
    }
        /***************************************************/
        /**** Interface Methods                         ****/
        /***************************************************/

        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            Type actualType = nominalType;

            // the BsonReader is sitting at the value whose actual type needs to be found
            BsonType bsonType = bsonReader.GetCurrentBsonType();

            if (bsonType == BsonType.Document)
            {
                var bookmark = bsonReader.GetBookmark();
                bsonReader.ReadStartDocument();

                if (bsonReader.FindElement(ElementName))
                {
                    var context       = BsonDeserializationContext.CreateRoot(bsonReader);
                    var discriminator = BsonValueSerializer.Instance.Deserialize(context);
                    if (discriminator.IsBsonArray)
                    {
                        discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator
                    }
                    if (BsonSerializer.IsTypeDiscriminated(nominalType))
                    {
                        actualType = BsonSerializer.LookupActualType(nominalType, discriminator);
                    }
                }
                bsonReader.ReturnToBookmark(bookmark);
            }

            return(actualType);
        }
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            if (nominalType == typeof(JobTask))
            {
                var ret = nominalType;

                var bookmark = bsonReader.GetBookmark();
                bsonReader.ReadStartDocument();
                if (bsonReader.FindElement(ElementName))
                {
                    var value = bsonReader.ReadString();

                    ret = Type.GetType(value);

                    if (ret == null)
                        throw new Exception("Could not find type " + value);

                    if (!ret.IsSubclassOf(typeof(JobTask)))
                        throw new Exception("Database type does not inherit from JobTask.");
                }

                bsonReader.ReturnToBookmark(bookmark);

                return ret;
            }
            else
            {
                return nominalType;
            }
        }
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            var bookmark = bsonReader.GetBookmark();
            bsonReader.ReadStartDocument();
            string typeValue = string.Empty;
            if (bsonReader.FindElement(ElementName))
                typeValue = bsonReader.ReadString();
            else
                throw new NotSupportedException();

            bsonReader.ReturnToBookmark(bookmark);
            var retr = Type.GetType(typeValue) ?? Type.GetType("ThreeOneThree.Proxima.Core.Entities." + typeValue);
            return retr;
        }
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            var bookmark = bsonReader.GetBookmark();

            bsonReader.ReadStartDocument();
            var t = nominalType;

            if (bsonReader.FindElement(ElementName))
            {
                var raw           = bsonReader.ReadString();
                var discriminator = _discriminatorMapper.Discriminator(raw);
                t = _discriminatorMapper.ConcreteType(discriminator);
            }
            bsonReader.ReturnToBookmark(bookmark);
            return(t);
        }
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            var ret = nominalType;

            var bookmark = bsonReader.GetBookmark();

            bsonReader.ReadStartDocument();
            if (bsonReader.FindElement(ElementName))
            {
                var value = bsonReader.ReadString();

                ret = Type.GetType(value);
            }

            bsonReader.ReturnToBookmark(bookmark);

            return(ret);
        }
        /***************************************************/
        /**** Interface Methods                         ****/
        /***************************************************/

        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            // the BsonReader is sitting at the value whose actual type needs to be found
            var bsonType = bsonReader.GetCurrentBsonType();

            if (bsonType == BsonType.Document)
            {
                var bookmark = bsonReader.GetBookmark();
                bsonReader.ReadStartDocument();
                var actualType = nominalType;
                if (bsonReader.FindElement(ElementName))
                {
                    var context       = BsonDeserializationContext.CreateRoot(bsonReader);
                    var discriminator = BsonValueSerializer.Instance.Deserialize(context);
                    try
                    {
                        actualType = BsonSerializer.LookupActualType(nominalType, discriminator);
                    }
                    catch
                    {
                        BsonDocument doc = new BsonDocument
                        {
                            { "_t", "System.Type" },
                            { "Name", discriminator.ToString() }
                        };

                        try
                        {
                            actualType = Convert.FromBson(doc) as Type;
                        }
                        catch { }
                    }
                }
                else
                {
                    actualType = typeof(CustomObject);
                }

                bsonReader.ReturnToBookmark(bookmark);
                return(actualType);
            }

            return(nominalType);
        }
Beispiel #15
0
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            var ret = nominalType;

            var bookmark = bsonReader.GetBookmark();

            bsonReader.ReadStartDocument();
            if (bsonReader.FindElement(ElementName))
            {
                var value = bsonReader.ReadString();

                ret = AppDomain.CurrentDomain.GetAssemblies()
                      .SelectMany(a => a.GetTypes().Where(a => a.IsSubclassOf(typeof(Node))))
                      .FirstOrDefault(a => a.FullName == value);
            }

            bsonReader.ReturnToBookmark(bookmark);

            return(ret);
        }
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            var bookmark = bsonReader.GetBookmark();

            bsonReader.ReadStartDocument();
            string typeValue = string.Empty;

            if (bsonReader.FindElement(ElementName))
            {
                typeValue = bsonReader.ReadString();
            }
            else
            {
                throw new NotSupportedException();
            }

            bsonReader.ReturnToBookmark(bookmark);
            var retr = Type.GetType(typeValue) ?? Type.GetType("ThreeOneThree.Proxima.Core.Entities." + typeValue);

            return(retr);
        }
Beispiel #17
0
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            var bookmark = bsonReader.GetBookmark();

            bsonReader.ReadStartDocument();
            string typeValue = string.Empty;

            if (bsonReader.FindElement(ElementName))
            {
                typeValue = bsonReader.ReadString();
            }
            else
            {
                throw new NotSupportedException();
            }

            bsonReader.ReturnToBookmark(bookmark);
            var result = Type.GetType(typeValue);

            return(result);
        }
        /// <inheritdoc/>
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            ThrowIfNominalTypeIsIncorrect(nominalType);
            var bookmark = bsonReader.GetBookmark();

            bsonReader.ReadStartDocument();
            Guid id = default;

            while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
            {
                switch (bsonReader.ReadName())
                {
                case Type:
                    var filterType = Enum.Parse <FilterType>(bsonReader.ReadString());
                    bsonReader.ReturnToBookmark(bookmark);
                    return(filterType switch
                    {
                        FilterType.EventTypeId => typeof(TypePartitionFilterDefinition),
                        FilterType.Remote => typeof(RemoteFilterDefinition),
                        _ => throw new UnsupportedFilterTypeEnumValue(filterType, id)
                    });
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            Type type     = null;
            var  bookmark = bsonReader.GetBookmark();

            bsonReader.ReadStartDocument();
            if (bsonReader.FindElement(ElementName))
            {
                var value = bsonReader.ReadString();
                if (typeMap.ContainsKey(value))
                {
                    type = typeMap[value];
                }
            }

            bsonReader.ReturnToBookmark(bookmark);
            if (type == null)
            {
                throw new Exception($"Type mis-configuration can't find bson type for ${nominalType}");
            }
            return(type);
        }
        /***************************************************/
        /**** Interface Methods                         ****/
        /***************************************************/

        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            Type actualType = nominalType;

            // the BsonReader is sitting at the value whose actual type needs to be found
            BsonType bsonType = bsonReader.GetCurrentBsonType();

            if (bsonType == BsonType.Document)
            {
                var bookmark = bsonReader.GetBookmark();
                bsonReader.ReadStartDocument();

                if (bsonReader.FindElement(ElementName))
                {
                    var context       = BsonDeserializationContext.CreateRoot(bsonReader);
                    var discriminator = BsonValueSerializer.Instance.Deserialize(context);
                    if (discriminator.IsBsonArray)
                    {
                        discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator
                    }
                    if (BsonSerializer.IsTypeDiscriminated(nominalType))
                    {
                        actualType = BsonSerializer.LookupActualType(nominalType, discriminator);
                    }
                    else if (Reflection.Compute.UnqualifiedName(nominalType.FullName) != Reflection.Compute.UnqualifiedName(discriminator.ToString()) && Config.AllowUpgradeFromBson && !Config.TypesWithoutUpgrade.Contains(actualType))
                    {
                        actualType = typeof(IDeprecated);
                    }
                }
                bsonReader.ReturnToBookmark(bookmark);

                if (Config.AllowUpgradeFromBson && actualType.IsDeprecated() && !Config.TypesWithoutUpgrade.Contains(actualType))
                {
                    actualType = typeof(IDeprecated);
                }
            }

            return(actualType);
        }
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            var ret = nominalType;

            var bookmark = bsonReader.GetBookmark();

            bsonReader.ReadStartDocument();
            if (bsonReader.FindElement(ElementName))
            {
                var value = bsonReader.ReadString();

                ret = Type.GetType(value);

                if (ret == null)
                {
                    throw new Exception("Could not find type " + value);
                }
            }

            bsonReader.ReturnToBookmark(bookmark);

            return(ret);
        }
Beispiel #22
0
        public virtual Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            var bookmark = bsonReader.GetBookmark();

            bsonReader.ReadStartDocument();
            if (!bsonReader.FindElement(ElementName))
            {
                bsonReader.ReturnToBookmark(bookmark);
                return(nominalType);
            }

            var typeValue = bsonReader.ReadString();

            bsonReader.ReturnToBookmark(bookmark);
            var type = GetItemType(new Guid(typeValue));

            if (type == null)
            {
                throw new MongoDerivedTypeResolutionException($"Cannot resolve derived type {typeValue}");
            }

            return(type);
        }
Beispiel #23
0
        internal static Type FindDocumentType(this IBsonReader reader, Type nominalType)
        {
            var bookmark   = reader.GetBookmark();
            var actualType = nominalType;

            reader.ReadStartDocument();

            if (reader.FindElement(Conventions.Type))
            {
                actualType = ReadActualType(reader, nominalType);
            }

            reader.ReturnToBookmark(bookmark);
            reader.ReadStartDocument();

            if (reader.FindElement(Conventions.TypeAlias))
            {
                actualType = ReadActualType(reader, nominalType);
            }

            reader.ReturnToBookmark(bookmark);
            return(actualType);
        }
 public Type GetActualType(IBsonReader bsonReader, Type nominalType)
 {
     var bookmark = bsonReader.GetBookmark();
     bsonReader.ReadStartDocument();
     var actualType = nominalType;
     while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
     {
         var name = bsonReader.ReadName();
         if (name == "OnlyInB")
         {
             actualType = typeof(B);
             break;
         }
         else if (name == "OnlyInC")
         {
             actualType = typeof(C);
             break;
         }
         bsonReader.SkipValue();
     }
     bsonReader.ReturnToBookmark(bookmark);
     return actualType;
 }
        // public methods
        /// <summary>
        /// Gets the actual type of an object by reading the discriminator from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The reader.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <returns>The actual type.</returns>
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            // the BsonReader is sitting at the value whose actual type needs to be found
            var bsonType = bsonReader.GetCurrentBsonType();
            if (bsonType == BsonType.Document)
            {
                // ensure KnownTypes of nominalType are registered (so IsTypeDiscriminated returns correct answer)
                BsonSerializer.EnsureKnownTypesAreRegistered(nominalType);

                // we can skip looking for a discriminator if nominalType has no discriminated sub types
                if (BsonSerializer.IsTypeDiscriminated(nominalType))
                {
                    var bookmark = bsonReader.GetBookmark();
                    bsonReader.ReadStartDocument();
                    var actualType = nominalType;
                    if (bsonReader.FindElement(_elementName))
                    {
                        var context = BsonDeserializationContext.CreateRoot(bsonReader);
                        var discriminator = BsonValueSerializer.Instance.Deserialize(context);
                        if (discriminator.IsBsonArray)
                        {
                            discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator
                        }
                        actualType = BsonSerializer.LookupActualType(nominalType, discriminator);
                    }
                    bsonReader.ReturnToBookmark(bookmark);
                    return actualType;
                }
            }

            return nominalType;
        }
        // public methods
        /// <summary>
        /// Gets the actual type of an object by reading the discriminator from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The reader.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <returns>The actual type.</returns>
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            // the BsonReader is sitting at the value whose actual type needs to be found
            var bsonType = bsonReader.GetCurrentBsonType();
            if (bsonReader.State == BsonReaderState.Value)
            {
                Type primitiveType = null;
                switch (bsonType)
                {
                    case BsonType.Boolean: primitiveType = typeof(bool); break;
                    case BsonType.Binary:
                        var bookmark = bsonReader.GetBookmark();
                        var binaryData = bsonReader.ReadBinaryData();
                        var subType = binaryData.SubType;
                        if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
                        {
                            primitiveType = typeof(Guid);
                        }
                        bsonReader.ReturnToBookmark(bookmark);
                        break;
                    case BsonType.DateTime: primitiveType = typeof(DateTime); break;
                    case BsonType.Decimal128: primitiveType = typeof(Decimal128); break;
                    case BsonType.Double: primitiveType = typeof(double); break;
                    case BsonType.Int32: primitiveType = typeof(int); break;
                    case BsonType.Int64: primitiveType = typeof(long); break;
                    case BsonType.ObjectId: primitiveType = typeof(ObjectId); break;
                    case BsonType.String: primitiveType = typeof(string); break;
                }

                // Type.IsAssignableFrom is extremely expensive, always perform a direct type check before calling Type.IsAssignableFrom
                if (primitiveType != null && (primitiveType == nominalType || nominalType.GetTypeInfo().IsAssignableFrom(primitiveType)))
                {
                    return primitiveType;
                }
            }

            if (bsonType == BsonType.Document)
            {
                var bookmark = bsonReader.GetBookmark();
                bsonReader.ReadStartDocument();
                var actualType = nominalType;
                if (bsonReader.FindElement(_elementName))
                {
                    var context = BsonDeserializationContext.CreateRoot(bsonReader);
                    var discriminator = BsonValueSerializer.Instance.Deserialize(context);
                    if (discriminator.IsBsonArray)
                    {
                        discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator
                    }
                    actualType = BsonSerializer.LookupActualType(nominalType, discriminator);
                }
                bsonReader.ReturnToBookmark(bookmark);
                return actualType;
            }

            return nominalType;
        }
Beispiel #27
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded)
        {
            BsonReaderBookmark Bookmark    = Reader.GetBookmark();
            BsonType?          DataTypeBak = DataType;

            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBsonType();
            }

            switch (DataType.Value)
            {
            case BsonType.Document:
                break;

            case BsonType.Boolean:
                return(Reader.ReadBoolean());

            case BsonType.Int32:
                return(Reader.ReadInt32());

            case BsonType.Int64:
                return(Reader.ReadInt64());

            case BsonType.Decimal128:
                return((decimal)Reader.ReadDecimal128());

            case BsonType.Double:
                return(Reader.ReadDouble());

            case BsonType.DateTime:
                return(ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime()));

            case BsonType.String:
            case BsonType.Symbol:
            case BsonType.JavaScript:
            case BsonType.JavaScriptWithScope:
                return(Reader.ReadString());

            case BsonType.Binary:
                return(Reader.ReadBytes());

            case BsonType.Null:
                Reader.ReadNull();
                return(null);

            default:
                throw new Exception("Object or value expected.");
            }

            LinkedList <KeyValuePair <string, object> > Properties = new LinkedList <KeyValuePair <string, object> >();
            LinkedList <KeyValuePair <string, object> > LowerCase  = null;
            string   TypeName       = string.Empty;
            Guid     ObjectId       = Guid.Empty;
            string   CollectionName = string.Empty;
            string   FieldName;
            BsonType ValueType;
            object   Value;

            Reader.ReadStartDocument();

            while (Reader.State == BsonReaderState.Type)
            {
                ValueType = Reader.ReadBsonType();
                if (ValueType == BsonType.EndOfDocument)
                {
                    break;
                }

                FieldName = Reader.ReadName();

                switch (ValueType)
                {
                case BsonType.Array:
                    Value = GeneratedObjectSerializerBase.ReadArray(null, this.Provider, Reader, ValueType);
                    break;

                case BsonType.Binary:
                    Value = Reader.ReadBytes();
                    break;

                case BsonType.Boolean:
                    Value = Reader.ReadBoolean();
                    break;

                case BsonType.DateTime:
                    Value = ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime());
                    break;

                case BsonType.Decimal128:
                    Value = (decimal)Reader.ReadDecimal128();
                    break;

                case BsonType.Document:
                    Value = this.Deserialize(Reader, ValueType, true);
                    break;

                case BsonType.Double:
                    Value = Reader.ReadDouble();
                    break;

                case BsonType.Int32:
                    Value = Reader.ReadInt32();
                    break;

                case BsonType.Int64:
                    Value = Reader.ReadInt64();
                    break;

                case BsonType.JavaScript:
                    Value = Reader.ReadJavaScript();
                    break;

                case BsonType.JavaScriptWithScope:
                    Value = Reader.ReadJavaScriptWithScope();
                    break;

                case BsonType.Null:
                    Value = null;
                    Reader.ReadNull();
                    break;

                case BsonType.ObjectId:
                    Value = Reader.ReadObjectId();
                    break;

                case BsonType.String:
                    Value = Reader.ReadString();
                    break;

                case BsonType.Symbol:
                    Value = Reader.ReadSymbol();
                    break;

                default:
                    throw new Exception("Unrecognized data type: " + ValueType.ToString());
                }

                switch (FieldName)
                {
                case "_id":
                    if (Value is Guid Guid)
                    {
                        ObjectId = Guid;
                    }
                    else if (Value is string s)
                    {
                        ObjectId = new Guid(s);
                    }
                    else if (Value is byte[] A)
                    {
                        ObjectId = new Guid(A);
                    }
                    else if (Value is ObjectId ObjId)
                    {
                        ObjectId = GeneratedObjectSerializerBase.ObjectIdToGuid(ObjId);
                    }
                    else
                    {
                        throw new Exception("Unrecognized Object ID type: " + Value.GetType().FullName);
                    }
                    break;

                case "_type":
                    TypeName = Value?.ToString();

                    if (this.returnTypedObjects && !string.IsNullOrEmpty(TypeName))
                    {
                        Type DesiredType = Types.GetType(TypeName);
                        if (DesiredType is null)
                        {
                            DesiredType = typeof(GenericObject);
                        }

                        if (DesiredType != typeof(GenericObject))
                        {
                            IObjectSerializer Serializer2 = this.provider.GetObjectSerializer(DesiredType);
                            Reader.ReturnToBookmark(Bookmark);
                            return(Serializer2.Deserialize(Reader, DataTypeBak, Embedded));
                        }
                    }
                    break;

                case "_collection":
                    CollectionName = Value?.ToString();
                    break;

                default:
                    if (FieldName.EndsWith("_L"))
                    {
                        string s      = FieldName.Substring(0, FieldName.Length - 2);
                        bool   Ignore = false;

                        foreach (KeyValuePair <string, object> P in Properties)
                        {
                            if (P.Key == s)
                            {
                                Ignore = true;
                                break;
                            }
                        }

                        if (!Ignore)
                        {
                            if (LowerCase is null)
                            {
                                LowerCase = new LinkedList <KeyValuePair <string, object> >();
                            }

                            LowerCase.AddLast(new KeyValuePair <string, object>(s, Value));
                        }
                    }
                    else
                    {
                        Properties.AddLast(new KeyValuePair <string, object>(FieldName, Value));
                    }
                    break;
                }
            }

            if (!(LowerCase is null))
            {
                foreach (KeyValuePair <string, object> P in LowerCase)
                {
                    bool Ignore = false;

                    foreach (KeyValuePair <string, object> P2 in Properties)
                    {
                        if (P2.Key == P.Key)
                        {
                            Ignore = true;
                            break;
                        }
                    }

                    if (!Ignore)
                    {
                        Properties.AddLast(new KeyValuePair <string, object>(P.Key + "_L", P.Value));
                    }
                }
            }

            Reader.ReadEndDocument();

            return(new GenericObject(CollectionName, TypeName, ObjectId, Properties));
        }
Beispiel #28
0
        /***************************************************/

        private object DeserializeDiscriminatedValue(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            // First try to recover the object type
            IBsonReader        reader   = context.Reader;
            BsonReaderBookmark bookmark = reader.GetBookmark();
            Type actualType             = typeof(CustomObject);

            try
            {
                actualType = _discriminatorConvention.GetActualType(reader, typeof(object));
            }
            catch
            {
                // This is were we can call the Version_Engine to return the new type string from the old one if exists
                string recordedType = GetCurrentTypeValue(reader);

                // If failed, return Custom object
                context.Reader.ReturnToBookmark(bookmark);
                Engine.Reflection.Compute.RecordWarning("The type " + recordedType + " is unknown -> data returned as custom objects.");
                IBsonSerializer customSerializer = BsonSerializer.LookupSerializer(typeof(CustomObject));
                return(customSerializer.Deserialize(context, args));
            }

            // Handle the special case where the type is object
            if (actualType == typeof(object))
            {
                BsonType currentBsonType = reader.GetCurrentBsonType();
                if (currentBsonType == BsonType.Document && context.DynamicDocumentSerializer != null)
                {
                    return(context.DynamicDocumentSerializer.Deserialize(context, args));
                }
                reader.ReadStartDocument();
                reader.ReadEndDocument();
                return(new object());
            }

            // Handle the genral case of finding the correct deserialiser and calling it
            IBsonSerializer            bsonSerializer            = BsonSerializer.LookupSerializer(actualType);
            IBsonPolymorphicSerializer bsonPolymorphicSerializer = bsonSerializer as IBsonPolymorphicSerializer;

            if (bsonPolymorphicSerializer != null && bsonPolymorphicSerializer.IsDiscriminatorCompatibleWithObjectSerializer)
            {
                bookmark = context.Reader.GetBookmark();
                try
                {
                    return(bsonSerializer.Deserialize(context, args));
                }
                catch
                {
                    context.Reader.ReturnToBookmark(bookmark);
                    Engine.Reflection.Compute.RecordWarning("Cannot find a definition of type " + actualType.FullName + " that matches the object to deserialise -> data returned as custom objects.");
                    IBsonSerializer customSerializer = BsonSerializer.LookupSerializer(typeof(CustomObject));
                    CustomObject    fallback         = customSerializer.Deserialize(context, args) as CustomObject;

                    //This is where we will try to get the correct object type from the custom object using the versionning engine

                    // If failed, just return the custom object
                    return(fallback);
                }
            }

            object result = null;
            bool   flag   = false;

            reader.ReadStartDocument();
            while (reader.ReadBsonType() != 0)
            {
                string text = reader.ReadName();
                if (text == _discriminatorConvention.ElementName)
                {
                    reader.SkipValue();
                }
                else
                {
                    if (!(text == "_v"))
                    {
                        throw new FormatException($"Unexpected element name: '{text}'.");
                    }
                    result = bsonSerializer.Deserialize(context);
                    flag   = true;
                }
            }
            reader.ReadEndDocument();
            if (!flag)
            {
                throw new FormatException("_v element missing.");
            }
            return(result);
        }
        public void TestBookmark()
        {
            var json = "{ \"x\" : 1, \"y\" : 2 }";

            using (_bsonReader = new JsonReader(json))
            {
                // do everything twice returning to bookmark in between
                var bookmark = _bsonReader.GetBookmark();
                Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());

                bookmark = _bsonReader.GetBookmark();
                _bsonReader.ReadStartDocument();
                _bsonReader.ReturnToBookmark(bookmark);
                _bsonReader.ReadStartDocument();

                bookmark = _bsonReader.GetBookmark();
                Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());

                bookmark = _bsonReader.GetBookmark();
                Assert.Equal("x", _bsonReader.ReadName());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal("x", _bsonReader.ReadName());

                bookmark = _bsonReader.GetBookmark();
                Assert.Equal(1, _bsonReader.ReadInt32());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal(1, _bsonReader.ReadInt32());

                bookmark = _bsonReader.GetBookmark();
                Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());

                bookmark = _bsonReader.GetBookmark();
                Assert.Equal("y", _bsonReader.ReadName());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal("y", _bsonReader.ReadName());

                bookmark = _bsonReader.GetBookmark();
                Assert.Equal(2, _bsonReader.ReadInt32());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal(2, _bsonReader.ReadInt32());

                bookmark = _bsonReader.GetBookmark();
                Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());

                bookmark = _bsonReader.GetBookmark();
                _bsonReader.ReadEndDocument();
                _bsonReader.ReturnToBookmark(bookmark);
                _bsonReader.ReadEndDocument();

                Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
            }
            Assert.Equal(json, BsonSerializer.Deserialize <BsonDocument>(json).ToJson());
        }
Beispiel #30
0
        /***************************************************/

        private object DeserializeDiscriminatedValue(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            // First try to recover the object type
            IBsonReader        reader   = context.Reader;
            BsonReaderBookmark bookmark = reader.GetBookmark();
            Type actualType             = typeof(CustomObject);

            try
            {
                actualType = _discriminatorConvention.GetActualType(reader, typeof(object));
            }
            catch
            {
                try
                {
                    context.Reader.ReturnToBookmark(bookmark);
                    DeprecatedSerializer deprecatedSerialiser = new DeprecatedSerializer();
                    return(deprecatedSerialiser.Deserialize(context, args));
                }
                catch { }
            }
            context.Reader.ReturnToBookmark(bookmark);

            if (actualType == null)
            {
                return(null);
            }

            // Handle the special case where the type is object
            if (actualType == typeof(object))
            {
                BsonType currentBsonType = reader.GetCurrentBsonType();
                if (currentBsonType == BsonType.Document && context.DynamicDocumentSerializer != null)
                {
                    return(context.DynamicDocumentSerializer.Deserialize(context, args));
                }
                reader.ReadStartDocument();
                reader.ReadEndDocument();
                return(new object());
            }

            // Handle the general case of finding the correct deserialiser and calling it
            try
            {
                if (!BsonClassMap.IsClassMapRegistered(actualType))
                {
                    Compute.RegisterClassMap(actualType); // LookupSerializer creates the classMap if it doesn't exist so important to do it through our own method
                }
                IBsonSerializer bsonSerializer = BsonSerializer.LookupSerializer(actualType);

                if (bsonSerializer.GetType().Name == "EnumerableInterfaceImplementerSerializer`2" && context.Reader.CurrentBsonType == BsonType.Document)
                {
                    if (!m_FallbackSerialisers.ContainsKey(actualType))
                    {
                        CreateFallbackSerialiser(actualType);
                    }
                    bsonSerializer = m_FallbackSerialisers[actualType];
                }
                else if (actualType.Name == "Dictionary`2" && context.Reader.CurrentBsonType == BsonType.Document)
                {
                    DictionarySerializer dicSerialiser = new DictionarySerializer();
                    return(dicSerialiser.Deserialize(context, args));
                }

                return(bsonSerializer.Deserialize(context, args));
            }
            catch (Exception e)
            {
                if (e.Message.Contains("Could not load file or assembly"))
                {
                    Engine.Reflection.Compute.RecordError(e.Message);
                }

                context.Reader.ReturnToBookmark(bookmark);
                DeprecatedSerializer deprecatedSerialiser = new DeprecatedSerializer();
                return(deprecatedSerialiser.Deserialize(context, args));
            }
        }
        /// <inheritdoc />
        public Type GetActualType(
            IBsonReader bsonReader,
            Type nominalType)
        {
            if (bsonReader == null)
            {
                throw new ArgumentNullException(nameof(bsonReader));
            }

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

            var bookmark = bsonReader.GetBookmark();

            bsonReader.ReadStartDocument();

            Type result;

            if (bsonReader.FindElement(this.ElementName))
            {
                var value = bsonReader.ReadString();

                try
                {
                    result = value.ResolveFromLoadedTypes();

                    if (result == null)
                    {
                        throw new InvalidOperationException(Invariant($"'{nameof(result)}' is null"));
                    }
                }
                catch (ArgumentException)
                {
                    bsonReader.ReturnToBookmark(bookmark);

                    // previously persisted documents will have used Type.Name
                    // in that case ToTypeRepresentationFromAssemblyQualifiedName will throw.
                    // this is here for backward compatibility.
                    result = HierarchicalDiscriminatorConvention.GetActualType(bsonReader, nominalType);

                    if (result == null)
                    {
                        throw new InvalidOperationException(Invariant($"Found discriminator '{value}' when deserializing into {nameof(nominalType)} '{nominalType.ToStringReadable()}', but could not get the actual type using {nameof(TypeNameDiscriminator)}.{nameof(TypeNameDiscriminator.GetActualType)}(); it returned null."));
                    }
                }
            }
            else
            {
                // if _t is not in the payload then a discriminator wasn't needed
                result = nominalType;
            }

            bsonReader.ReturnToBookmark(bookmark);

            // See notes in ThrowOnUnregisteredTypeIfAppropriate for the need to make this call.
            // Note that this is a sub-par solution.  Ideally this discriminator would know
            // which serializer (and hence which serialization configuration) is being used for deserializing,
            // because it is passed as a parameter to this method.  Because that's not an option in Mongo,
            // we have to use ObcBsonSerializer.SerializationConfigurationInUseForDeserialization, which
            // tracks the thread being used for the deserialize operation and associates the serialization
            // configuration in-use with the thread.
            var serializationConfiguration = ObcBsonSerializer.GetSerializationConfigurationInUseForDeserialization();

            // serializationConfiguration is only ever null if the consumer is NOT using the front-door for serialization
            // (i.e. a serializer), but using the Mongo driver directly to deserialize.  In that case, we do not know
            // which serialization configuration is "in use".
            serializationConfiguration?.ThrowOnUnregisteredTypeIfAppropriate(result, SerializationDirection.Deserialize, null);

            return(result);
        }
        /***************************************************/

        private object DeserializeDiscriminatedValue(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            // First try to recover the object type
            IBsonReader        reader   = context.Reader;
            BsonReaderBookmark bookmark = reader.GetBookmark();
            Type actualType             = typeof(CustomObject);

            try
            {
                actualType = _discriminatorConvention.GetActualType(reader, typeof(object));
            }
            catch (Exception e)
            {
                BsonDocument doc = null;

                try
                {
                    context.Reader.ReturnToBookmark(bookmark);
                    IBsonSerializer bSerializer = BsonSerializer.LookupSerializer(typeof(BsonDocument));
                    doc = bSerializer.Deserialize(context, args) as BsonDocument;
                }
                catch { }

                if (doc != null && doc.Contains("_t") && doc["_t"].AsString == "DBNull")
                {
                    return(null);
                }
                else
                {
                    actualType = typeof(IDeprecated);
                }
            }
            context.Reader.ReturnToBookmark(bookmark);

            if (actualType == null)
            {
                return(null);
            }

            // Make sure the type is not deprecated
            if (Config.AllowUpgradeFromBson && actualType.IIsDeprecated() && !Config.TypesWithoutUpgrade.Contains(actualType))
            {
                actualType = typeof(IDeprecated);
            }

            // Handle the special case where the type is object
            if (actualType == typeof(object))
            {
                BsonType currentBsonType = reader.GetCurrentBsonType();
                if (currentBsonType == BsonType.Document && context.DynamicDocumentSerializer != null)
                {
                    return(context.DynamicDocumentSerializer.Deserialize(context, args));
                }
                reader.ReadStartDocument();
                reader.ReadEndDocument();
                return(new object());
            }

            // Handle the general case of finding the correct deserialiser and calling it
            try
            {
                IBsonSerializer bsonSerializer = BsonSerializer.LookupSerializer(actualType);

                if (bsonSerializer.GetType().Name == "EnumerableInterfaceImplementerSerializer`2" && context.Reader.CurrentBsonType == BsonType.Document)
                {
                    if (!m_FallbackSerialisers.ContainsKey(actualType))
                    {
                        CreateFallbackSerialiser(actualType);
                    }
                    bsonSerializer = m_FallbackSerialisers[actualType];
                }

                return(bsonSerializer.Deserialize(context, args));
            }
            catch (Exception e)
            {
                context.Reader.ReturnToBookmark(bookmark);

                if (e is FormatException && e.InnerException != null && (e.InnerException is FormatException || e.InnerException is BsonSerializationException))
                {
                    // A child of the object is causing problems. Try to recover from custom object
                    IBsonSerializer customSerializer = BsonSerializer.LookupSerializer(typeof(CustomObject));
                    object          result           = customSerializer.Deserialize(context, args);
                    Guid            objectId         = ((CustomObject)result).BHoM_Guid;

                    if (!Config.TypesWithoutUpgrade.Contains(actualType))
                    {
                        if (m_StackCounter.ContainsKey(objectId))
                        {
                            m_StackCounter[objectId] += 1;
                        }
                        else
                        {
                            m_StackCounter[objectId] = 1;
                        }

                        if (m_StackCounter[objectId] < 10)
                        {
                            result = Convert.FromBson(result.ToBson());
                            m_StackCounter.Remove(objectId);
                        }
                    }

                    if (result is CustomObject)
                    {
                        Engine.Reflection.Compute.RecordWarning("The type " + actualType.FullName + " is unknown -> data returned as custom objects.");
                        Config.TypesWithoutUpgrade.Add(actualType);
                    }

                    return(result);
                }
                else if (actualType != typeof(IDeprecated))
                {
                    // Try the deprecated object serialiser
                    IBsonSerializer deprecatedSerializer = BsonSerializer.LookupSerializer(typeof(IDeprecated));
                    return(deprecatedSerializer.Deserialize(context, args));
                }
                else
                {
                    // Last resort: just return the custom object
                    Engine.Reflection.Compute.RecordWarning("The type " + actualType.FullName + " is unknown -> data returned as custom objects.");
                    IBsonSerializer customSerializer = BsonSerializer.LookupSerializer(typeof(CustomObject));
                    return(customSerializer.Deserialize(context, args));
                }
            }
        }
        public void TestBookmark()
        {
            var json = "{ \"x\" : 1, \"y\" : 2 }";
            using (_bsonReader = new JsonReader(json))
            {
                // do everything twice returning to bookmark in between
                var bookmark = _bsonReader.GetBookmark();
                Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());

                bookmark = _bsonReader.GetBookmark();
                _bsonReader.ReadStartDocument();
                _bsonReader.ReturnToBookmark(bookmark);
                _bsonReader.ReadStartDocument();

                bookmark = _bsonReader.GetBookmark();
                Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());

                bookmark = _bsonReader.GetBookmark();
                Assert.Equal("x", _bsonReader.ReadName());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal("x", _bsonReader.ReadName());

                bookmark = _bsonReader.GetBookmark();
                Assert.Equal(1, _bsonReader.ReadInt32());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal(1, _bsonReader.ReadInt32());

                bookmark = _bsonReader.GetBookmark();
                Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());

                bookmark = _bsonReader.GetBookmark();
                Assert.Equal("y", _bsonReader.ReadName());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal("y", _bsonReader.ReadName());

                bookmark = _bsonReader.GetBookmark();
                Assert.Equal(2, _bsonReader.ReadInt32());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal(2, _bsonReader.ReadInt32());

                bookmark = _bsonReader.GetBookmark();
                Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
                _bsonReader.ReturnToBookmark(bookmark);
                Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());

                bookmark = _bsonReader.GetBookmark();
                _bsonReader.ReadEndDocument();
                _bsonReader.ReturnToBookmark(bookmark);
                _bsonReader.ReadEndDocument();

                Assert.Equal(BsonReaderState.Initial, _bsonReader.State);

            }
            Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
        }
        // public methods
        /// <summary>
        /// Gets the actual type of an object by reading the discriminator from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The reader.</param>
        /// <param name="nominalType">The nominal type.</param>
        /// <returns>The actual type.</returns>
        public Type GetActualType(IBsonReader bsonReader, Type nominalType)
        {
            // the BsonReader is sitting at the value whose actual type needs to be found
            var bsonType = bsonReader.GetCurrentBsonType();

            if (bsonReader.State == BsonReaderState.Value)
            {
                Type primitiveType = null;
                switch (bsonType)
                {
                case BsonType.Boolean: primitiveType = typeof(bool); break;

                case BsonType.Binary:
                    var bookmark   = bsonReader.GetBookmark();
                    var binaryData = bsonReader.ReadBinaryData();
                    var subType    = binaryData.SubType;
                    if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
                    {
                        primitiveType = typeof(Guid);
                    }
                    bsonReader.ReturnToBookmark(bookmark);
                    break;

                case BsonType.DateTime: primitiveType = typeof(DateTime); break;

                case BsonType.Decimal128: primitiveType = typeof(Decimal128); break;

                case BsonType.Double: primitiveType = typeof(double); break;

                case BsonType.Int32: primitiveType = typeof(int); break;

                case BsonType.Int64: primitiveType = typeof(long); break;

                case BsonType.ObjectId: primitiveType = typeof(ObjectId); break;

                case BsonType.String: primitiveType = typeof(string); break;
                }

                // Type.IsAssignableFrom is extremely expensive, always perform a direct type check before calling Type.IsAssignableFrom
                if (primitiveType != null && (primitiveType == nominalType || nominalType.GetTypeInfo().IsAssignableFrom(primitiveType)))
                {
                    return(primitiveType);
                }
            }

            if (bsonType == BsonType.Document)
            {
                var bookmark = bsonReader.GetBookmark();
                bsonReader.ReadStartDocument();
                var actualType = nominalType;
                if (bsonReader.FindElement(_elementName))
                {
                    var context       = BsonDeserializationContext.CreateRoot(bsonReader);
                    var discriminator = BsonValueSerializer.Instance.Deserialize(context);
                    if (discriminator.IsBsonArray)
                    {
                        discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator
                    }
                    actualType = BsonSerializer.LookupActualType(nominalType, discriminator);
                }
                bsonReader.ReturnToBookmark(bookmark);
                return(actualType);
            }

            return(nominalType);
        }