public override void Serialize(
BsonWriter bsonWriter,
Type nominalType,
object value,
IBsonSerializationOptions options)
{
var objectId = (ObjectId)value;
var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);
switch (representationSerializationOptions.Representation)
{
case BsonType.ObjectId:
bsonWriter.WriteObjectId(objectId);
break;
case BsonType.String:
bsonWriter.WriteString(objectId.ToString());
break;
default:
var message = string.Format("'{0}' is not a valid ObjectId representation.", representationSerializationOptions.Representation);
throw new BsonSerializationException(message);
}
}
}