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