public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(byte[]));
BsonType bsonType = bsonReader.GetCurrentBsonType();
byte[] bytes;
string message;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Binary:
bytes = bsonReader.ReadBytes();
return bytes;
case BsonType.String:
var s = bsonReader.ReadString();
if ((s.Length % 2) != 0)
{
s = "0" + s; // prepend a zero to make length even
}
bytes = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
{
var hex = s.Substring(i, 2);
var b = byte.Parse(hex, NumberStyles.HexNumber);
bytes[i / 2] = b;
}
return bytes;
default:
message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType);
throw new Exception(message);
}
}
#pragma warning restore 618