Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-4791

Driver unable to deserialise valid decimal values

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Summary

      When deserialising certain valid numbers to System.Decimal (e.g. 9.300000190734863 ) an exception of MongoDB.Bson.TruncationException is thrown

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      Assembly: MongoDB.Bson, Version=2.21.0.0, Culture=neutral, PublicKeyToken=null

      How to Reproduce

      Here's some sample code that should work just fine but does not:

      public class MyDocumentType {
         public decimal Number {get;set;}
      }
      
      public static void Main() {
          var validDecimalString = "9.300000190734863";
          var validDecimal = Decimal.Parse(validDecimalString);
          Console.WriteLine(validDecimalString + " --> " + validDecimal);
          Console.WriteLine();
      
          var myValidDecimalJson = "{\"Number\": 9.300000190734863 }";
          try 
          {
              var obj = BsonSerializer.Deserialize<MyDocumentType>(myValidDecimalJson);
              Console.WriteLine(myValidDecimalJson + " --> " + obj.Number);
          }
          catch (Exception e)
          {
              Console.WriteLine(myValidDecimalJson + " ---> " + e.GetType().Name + ": " + e.Message);
          }
      
      
      //Sample output:
      // 9.300000190734863 --> 9.300000190734863
      //
      // {"Number": 9.300000190734863 } ---> FormatException: An error occurred while deserializing the Number property of class VetDB.Tests.My.SampleDeserialisations+MyDocumentType: Truncation resulted in data loss.
      
      
      

      Additional Background

      Please provide any additional background information that may be helpful in diagnosing the bug.

            Assignee:
            oleksandr.poliakov@mongodb.com Oleksandr Poliakov
            Reporter:
            rhys.bevilaqua@vetdb.com Rhys Bevilaqua
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: