MongoDB.Bson.Serialization.Serializers.BsonDocumentSerializer.Deserialize C# (CSharp) Метод

Deserialize() публичный метод

Deserializes an object from a BsonReader.
public Deserialize ( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options ) : object
bsonReader BsonReader The BsonReader.
nominalType System.Type The nominal type of the object.
actualType System.Type The actual type of the object.
options IBsonSerializationOptions The serialization options.
Результат object
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            VerifyTypes(nominalType, actualType, typeof(BsonDocument));

            var bsonType = bsonReader.GetCurrentBsonType();
            string message;
            switch (bsonType)
            {
                case BsonType.Document:
                    var documentSerializationOptions = (options ?? DocumentSerializationOptions.Defaults) as DocumentSerializationOptions;
                    if (documentSerializationOptions == null)
                    {
                        message = string.Format(
                            "Serialize method of BsonDocument expected serialization options of type {0}, not {1}.",
                            BsonUtils.GetFriendlyTypeName(typeof(DocumentSerializationOptions)),
                            BsonUtils.GetFriendlyTypeName(options.GetType()));
                        throw new BsonSerializationException(message);
                    }

                    bsonReader.ReadStartDocument();
                    var document = new BsonDocument(documentSerializationOptions.AllowDuplicateNames);
                    while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
                    {
                        var name = bsonReader.ReadName();
                        var value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
                        document.Add(name, value);
                    }
                    bsonReader.ReadEndDocument();

                    return document;
                default:
                    message = string.Format("Cannot deserialize BsonDocument from BsonType {0}.", bsonType);
                    throw new Exception(message);
            }
        }