public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(Version));
BsonType bsonType = bsonReader.GetCurrentBsonType();
string message;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Document:
bsonReader.ReadStartDocument();
int major = -1, minor = -1, build = -1, revision = -1;
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
var name = bsonReader.ReadName();
switch (name)
{
case "Major": major = bsonReader.ReadInt32(); break;
case "Minor": minor = bsonReader.ReadInt32(); break;
case "Build": build = bsonReader.ReadInt32(); break;
case "Revision": revision = bsonReader.ReadInt32(); break;
default:
message = string.Format("Unrecognized element '{0}' while deserializing a Version value.", name);
throw new Exception(message);
}
}
bsonReader.ReadEndDocument();
if (major == -1)
{
message = string.Format("Version missing Major element.");
throw new Exception(message);
}
else if (minor == -1)
{
message = string.Format("Version missing Minor element.");
throw new Exception(message);
}
else if (build == -1)
{
return new Version(major, minor);
}
else if (revision == -1)
{
return new Version(major, minor, build);
}
else
{
return new Version(major, minor, build, revision);
}
case BsonType.String:
return new Version(bsonReader.ReadString());
default:
message = string.Format("Cannot deserialize Version from BsonType {0}.", bsonType);
throw new Exception(message);
}
}