[CSHARP-35] BsonWriter.WriteValue throws System.InvalidCastException when serializing a float field. Created: 01/Apr/10  Updated: 01/Apr/10  Resolved: 01/Apr/10

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

Type: Bug Priority: Critical - P2
Reporter: Doug Marien Assignee: Sam Corder
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Using pre-built binaries from 0.082.1.



 Description   

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);
}



 Comments   
Comment by Sam Corder [ 01/Apr/10 ]

Will be released as part of 0.82.2

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