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

Error in deserializing Decimal stored as object in driver 2.19

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      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?

      Summary

      After upgrading to driver 2.19 we noticed integration test starts failing. The reason is in how the driver changed serialization of Decimal when saved as object.

      We have a .NET type that has a type Value that contains an Object, then we can store different type and Bson Serialization does the magic. With old version of the driver here is how data is stored in MongoDb

      "Value" : {
           "_t" : "System.Decimal",
           "_v" : "12"
      },

      When the data is deserialized we have a System.Decimal with value 12 in the property. Upgrading to 2.19 version of the driver we have this in the database

       "Value" : NumberDecimal("12"),

      And the problem is that after deserialization we have value property with an object of type MongoDb.Bson.Decimal128. This breaks the software because it cannot be casted to decimal.

      As you can see the check "is Decimal" fails also it cannot be casted to Decimal, but we need to use Convert.ToDecimal(). This is actually breaking our software that is based on pure .NET type checking.

      From my point of view Serialization is broken because I save object with a decimal and I got back object of Bson.Decimal128.

      Version of driver 2.19

      How to Reproduce

      MongoTest sut = new MongoTest() { Value = 12M };
      Console.WriteLine(sut.Value is Decimal);
      var serialized = sut.ToBson();
      var deserialized = BsonSerializer.Deserialize<MongoTest>(serialized);
      Console.WriteLine(deserialized.Value is Decimal);

      public class MongoTest 

      {     public Object Value \{ get; set; }

      }

      As you can see from the above code, I serialize an object with a decimal in the Value property, when I deserialize I have an internal class that is not Decimal. This actually breaks serialization.

       

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            alkampfer@nablasoft.com Gian Maria Ricci
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: