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

BsonWriter.WriteValue throws System.InvalidCastException when serializing a float field.

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:
      Using pre-built binaries from 0.082.1.

      When attempting to serialize a Document with a float field, BsonWriter.TranslateToBsonType returns BsonDataType.Number for System.Single types.

      In BsonWriter.WriteValue, an attempt is made to cast the object to double which is invalid since the object is of type System.Single:

      case BsonDataType.Number:
      writer.Write((double)obj);
      return;

      Here's a good article on the why http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx

      One possible fix would be:

      if (obj is System.Single) {
      writer.Write((double)(float)obj);
      } else {
      writer.Write((double)obj);
      }

            Assignee:
            sam Sam Corder
            Reporter:
            dmarien Doug Marien
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: