public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(IPEndPoint));
BsonType bsonType = bsonReader.GetCurrentBsonType();
string message;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.String:
var stringValue = bsonReader.ReadString();
var match = Regex.Match(stringValue, @"^(?<address>(.+|\[.*\]))\:(?<port>\d+)$");
if (match.Success)
{
IPAddress address;
if (IPAddress.TryParse(match.Groups["address"].Value, out address))
{
int port;
if (int.TryParse(match.Groups["port"].Value, out port))
{
return new IPEndPoint(address, port);
}
}
}
message = string.Format("Invalid IPEndPoint value '{0}'.", stringValue);
throw new Exception(message);
default:
message = string.Format("Cannot deserialize IPEndPoint from BsonType {0}.", bsonType);
throw new Exception(message);
}
}