public override void Serialize(
BsonWriter bsonWriter,
Type nominalType,
object value,
IBsonSerializationOptions options)
{
var byteValue = (byte)value;
var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options);
switch (representationSerializationOptions.Representation)
{
case BsonType.Binary:
bsonWriter.WriteBytes(new byte[] { byteValue });
break;
case BsonType.Int32:
bsonWriter.WriteInt32(byteValue);
break;
case BsonType.Int64:
bsonWriter.WriteInt64(byteValue);
break;
case BsonType.String:
bsonWriter.WriteString(string.Format("{0:x2}", byteValue));
break;
default:
var message = string.Format("'{0}' is not a valid Byte representation.", representationSerializationOptions.Representation);
throw new BsonSerializationException(message);
}
}
}