[CSHARP-1452] BsonDocument which is null actually create an object with one name-value pair Created: 18/Oct/15  Updated: 03/Dec/20  Resolved: 03/Dec/20

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

Type: Task Priority: Major - P3
Reporter: Itzhak Kagan Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

windows 7 64 bit WiredTiger engine


Issue Links:
Duplicate
duplicates CSHARP-863 How should BsonValues be serialized f... Closed

 Description   

If I create a property in a class like:

public class User
{
    public string Id { get; set; }
    public BsonDocument UserSettings { get; set; }  
    .....
}

If the UserSettings is null when a new document is inserted then the driver actually creates a bson document with one name-value pair like that:

"userSettings" : {
    "_csharpnull" : true
},

That looks strange to me since i expected that property to be null like that:

"userSettings" : null,

Am I missing something?

Thanks,
Itzik



 Comments   
Comment by Itzhak Kagan [ 19/Oct/15 ]

Thanks Robert,

I read the CSHARP-863 ticket.
There is an issue however. If the c# client is the only client then there is no problem but if there there are more clients (that at least one of them is not c#) that both add data and do analysis on it, then you come up with tow different values that actually meant to be same. If other clients will query that value if it is null they will be surprised to find out that it is not.
I think that it is a point to think about.

Thanks,
Itzik

Comment by Robert Stam [ 18/Oct/15 ]

I understand that this would look strange to you.

I encourage you to read the description and comments on the CSHARP-863 ticket where this is discussed in detail.

The problem traces back to how to represent the difference between a BsonValue whose value is a BSON null vs a BsonValue whose value is is C# null. The difference is illustrated here:

public class C
{
    public BsonValue V;
}
 
var c1 = new C { V = BsonNull.Value };
var c2 = new C { V = null };
 
var json1 = c1.ToJson(); // { v : null }
var json2 = c2.ToJson(); // { v : { __csharpnull : true } }

These two different representations are necessary in order to successfully round trip the two different values.

While this example is using BsonValue as the type for V, the decision was made to treat all subclasses of BsonValue (including BsonDocument) the same way.

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