public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
var bsonType = bsonReader.GetCurrentBsonType();
if (bsonType == BsonType.Document && IsCSharpNullRepresentation(bsonReader))
{
// if IsCSharpNullRepresentation returns true it will have consumed the document representing C# null
return null;
}
// handle BSON null for backward compatibility with existing data (new data would have _csharpnull)
if (bsonType == BsonType.Null && (nominalType != typeof(BsonValue) && nominalType != typeof(BsonNull)))
{
bsonReader.ReadNull();
return null;
}
var serializer = BsonSerializer.LookupSerializer(actualType);
return serializer.Deserialize(bsonReader, nominalType, actualType, options);
}