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