|
Values of type System.Decimal are serialized as strings for two reasons:
1. The range of System.Decimal is different from that of BSON Decimal128 so overflow or loss of precision is possible
2. For backward compatibility (the driver has been serializing System.Decimal as string since before the BSON Decimal128 type was introduced)
If you want to configure your application to serialize System.Decimal as BSON Decimal128 you can do so by adding the following configuration code to your application:
var decimalSerializer = new DecimalSerializer(BsonType.Decimal128, new RepresentationConverter(allowOverflow: false, allowTruncation: false));
|
BsonSerializer.RegisterSerializer(decimalSerializer);
|
You must register this serializer as early as possible. At a minimum it must be registered before any System.Decimal values are serialized.
|