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);
}
}