[CSHARP-285] Decimal serialization/deserialization should not be affected by locale Created: 26/Jul/11  Updated: 02/Apr/15  Resolved: 26/Jul/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.1
Fix Version/s: 1.1

Type: Bug Priority: Trivial - P5
Reporter: Antoine Leclair Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 7, .NET 4, C# driver v1.1, environment setting for decimal separator is a comma ","



 Description   

The decimal separator in my regional settings is a comma ",". When I store decimal variables, the driver saves them as string (which seems to be by design). But the problem is that the string contains a comma for the separator and it throws an exception when the object is deserialized. For example, the number 3.25 appears as the string "3,25" in the database and won't deserialize.

Also, I tried to add [BsonRepresentation(BsonType.Double)], but the representation in the database is still a string containing a comma. This may be another bug by itself.

Here's the stack trace of one of my unit test failing:

System.FormatException : Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, ref NumberBuffer number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)
at MongoDB.Bson.Serialization.Serializers.DecimalSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) in C:\work\10gen\mongodb\mongo-csharp-driver\Bson\Serialization\Serializers\NetPrimitiveSerializers.cs: line 744
at MongoDB.Bson.Serialization.Serializers.BsonBaseSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) in C:\work\10gen\mongodb\mongo-csharp-driver\Bson\Serialization\Serializers\BsonBaseSerializer.cs: line 69
at MongoDB.Bson.Serialization.BsonClassMapSerializer.DeserializeMember(BsonReader bsonReader, Object obj, BsonMemberMap memberMap) in C:\work\10gen\mongodb\mongo-csharp-driver\Bson\Serialization\BsonClassMapSerializer.cs: line 305
at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) in C:\work\10gen\mongodb\mongo-csharp-driver\Bson\Serialization\BsonClassMapSerializer.cs: line 127
at MongoDB.Bson.Serialization.BsonClassMapSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) in C:\work\10gen\mongodb\mongo-csharp-driver\Bson\Serialization\BsonClassMapSerializer.cs: line 81
at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) in C:\work\10gen\mongodb\mongo-csharp-driver\Bson\Serialization\BsonSerializer.cs: line 217
at MongoDB.Driver.Internal.MongoReplyMessage`1.ReadFrom(BsonBuffer buffer, IBsonSerializationOptions serializationOptions) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Internal\MongoReplyMessage.cs: line 94
at MongoDB.Driver.Internal.MongoConnection.ReceiveMessage(BsonBinaryReaderSettings readerSettings, IBsonSerializationOptions serializationOptions) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Internal\MongoConnection.cs: line 337
at MongoDB.Driver.MongoCursorEnumerator`1.GetReply(MongoConnection connection, MongoRequestMessage message) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs: line 263
at MongoDB.Driver.MongoCursorEnumerator`1.GetFirst() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs: line 223
at MongoDB.Driver.MongoCursorEnumerator`1.MoveNext() in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCursorEnumerator.cs: line 126
at System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source)
at MongoDB.Driver.MongoCollection.FindOneAs(IMongoQuery query) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCollection.cs: line 493
at MongoDB.Driver.MongoCollection.FindOneByIdAs(BsonValue id) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCollection.cs: line 529
at MongoDB.Driver.MongoCollection`1.FindOneById(BsonValue id) in C:\work\10gen\mongodb\mongo-csharp-driver\Driver\Core\MongoCollection.cs: line 1462
at Domain.Storage.MongoSession.Single(ObjectId id) in MongoSession.cs: line 81
at WebApp.Test.Controllers.RatingControllerTest.Rate_RecalculatesRating() in RatingControllerTest.cs: line 131



 Comments   
Comment by Robert Stam [ 26/Jul/11 ]

No problem. I'm happy to check it out. Could easily have been a real problem. Let me know if after further review you find anything else you want me to look at.

Comment by Antoine Leclair [ 26/Jul/11 ]

I tested your code, it works fine.

I am terribly sorry, it must be some kind of abstraction layer we use that caused the bug.

Comment by Robert Stam [ 26/Jul/11 ]

I can't reproduce this. When Decimal is converted to a string it uses XmlConvert.ToString which is not culture sensitive. String is the default representation for Decimal because BSON has no equivalent data type.

Here's my test program:

http://www.pastie.org/2274489

And here's the output (note that the string representation of Decimal is using a "." even though the CurrentCulture was set to German):

{ "_id" : ObjectId("4e2ede14e447ad2bd0ed5d78"), "Culture" : "German (Germany)", "Decimal" : "1.23" }

Press Enter to continue

Changing the representation in your code does not affect existing data, so any strings with commas already in the database are there to stay and will have to be either removed or fixed using some other process.

Generated at Wed Feb 07 21:36:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.