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

[BsonDefaultValue(null, SerializeDefaultValue = false)] doesn't work as expected

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • 1.0
    • 0.9
    • None
    • None
    • Win 7

    Description

      If I have an object with a null on it for a property that is marked "[BsonDefaultValue(null, SerializeDefaultValue = false)]", I'd expect that no value would get written to the database. But what happens is that the test code to decide whether null == null blows up because of the way it is written.

      Looking at the code I can see that I should have used [BsonIgnoreIfNull] but why isn't [BsonIgnoreIfNull] equivalent to [BsonDefaultValue(null, SerializeDefaultValue = false)]?

      Suggest that with a small tweak this code could be made to work whichever way a user chooses to specify that null values are the default and are to be ignored.

      private void SerializeMember(
      BsonWriter bsonWriter,
      object obj,
      BsonMemberMap memberMap
      ) {
      var value = memberMap.Getter(obj);
      if (value == null && memberMap.IgnoreIfNull)

      { /// SUGGEST: Make this work if memberMap.DefaultValue == null too ... return; // don't serialize null value }

      if (memberMap.HasDefaultValue && !memberMap.SerializeDefaultValue && value.Equals(memberMap.DefaultValue))

      { return; // don't serialize default value }

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            ianmercer Ian Mercer
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: