Bson document nullable type handling

123 views
Skip to first unread message

Todd Harvey

unread,
Jul 26, 2012, 4:46:08 PM7/26/12
to mongodb...@googlegroups.com
Hi,
  just playing around with the BsonDocument as I was hoping to use it as a way to send a serialized object around as a string.  

First thing I found is that it does not support decimals which may be a show stopper as I need it for a financial app.

Second thing may just be me not understanding how to deal with nullable types

the following unit test fails with "Invalid BsonType 253." on the deserialization.  Does this look like a bug or am I using things wrong.

       [Test]
        public void TestNullables()
        {
            int? age = null;
            DateTime? dt = null;
            //double? dec = null;
            //string str = null;
            var nullRow = new BsonDocument()
                .Add("Age", age)
                .Add("DateOfBirth", dt);
                    //.Add("NetWorth", dec)
                    //.Add("Name", str);
            string bson = GetBsonString(nullRow);
            var bsondoc = GetBsonDocument(bson); // crashes 

        }
        
        private BsonDocument GetBsonDocument(string bsonString)
        {
            var bytes = Encoding.Unicode.GetBytes(bsonString);
            var bsonDoc = BsonSerializer.Deserialize<BsonDocument>(bytes);
            return bsonDoc;
        }
        private string GetBsonString(BsonDocument bson)
        {
            var bytes = bson.ToBson();

            var bsonString = Encoding.Unicode.GetString(bytes);
            return bsonString;
            
        }


Stack trace
   at MongoDB.Bson.IO.BsonBuffer.ReadBsonType() in C:\source\mongo-csharp-driver\Bson\IO\BsonBuffer.cs:line 378
   at MongoDB.Bson.IO.BsonBinaryReader.ReadBsonType[TValue](BsonTrie`1 bsonTrie, Boolean& found, TValue& value) in C:\source\mongo-csharp-driver\Bson\IO\BsonBinaryReader.cs:line 184
   at MongoDB.Bson.IO.BsonReader.ReadBsonType() in C:\source\mongo-csharp-driver\Bson\IO\BsonReader.cs:line 375
   at MongoDB.Bson.BsonElement.ReadFrom(BsonReader bsonReader, BsonElement& element) in C:\source\mongo-csharp-driver\Bson\ObjectModel\BsonElement.cs:line 164
   at MongoDB.Bson.BsonDocument.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) in C:\source\mongo-csharp-driver\Bson\ObjectModel\BsonDocument.cs:line 781
   at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) in C:\source\mongo-csharp-driver\Bson\Serialization\BsonSerializer.cs:line 211
   at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(BsonReader bsonReader, Type nominalType) in C:\source\mongo-csharp-driver\Bson\Serialization\BsonSerializer.cs:line 196
   at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(Stream stream, Type nominalType) in C:\source\mongo-csharp-driver\Bson\Serialization\BsonSerializer.cs:line 258
   at MongoDB.Bson.Serialization.BsonSerializer.Deserialize(Byte[] bytes, Type nominalType) in C:\source\mongo-csharp-driver\Bson\Serialization\BsonSerializer.cs:line 244
   at MongoDB.Bson.Serialization.BsonSerializer.Deserialize[TNominalType](Byte[] bytes) in C:\source\mongo-csharp-driver\Bson\Serialization\BsonSerializer.cs:line 124
   at Bm.Core.Helpers.Common.GetBsonDocument(String bsonString) in C:\Users\tfh\Documents\Development\DataBridge\Bm.Core\Helpers\Common.cs:line 559
   at Bm.Tests.WebsiteSmokeTests.CreateTestBsonDocument() in C:\Users\tfh\Documents\Development\DataBridge\Bm.Tests\WebsiteSmokeTests.cs:line 166        

Robert Stam

unread,
Jul 26, 2012, 4:55:30 PM7/26/12
to mongodb...@googlegroups.com
BSON is a binary format and you shouldn't expect to be able to expect to convert it to a string and back, at least not by assuming that the binary bytes represent a valid Unicode byte stream, which they do not. You could possibly convert the binary bytes to a hex string or a base64 string and back without loss of fidelity.

BsonDocument does not support the decimal type because the corresponding BSON spec doesn't either. What you could do is store the decimal values as strings in the database.

If you want to send around a serialized object as a string you could consider JSON instead of BSON, either the JSON supported by the MongoDB C# driver or some other JSON library.
Reply all
Reply to author
Forward
0 new messages