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

New [BsonIgnoreIfNoElements] attribute

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Priority: Minor - P4
    • Resolution: Works as Designed
    • Affects Version/s: 1.8.3
    • Fix Version/s: None
    • Component/s: Feature Request
    • Labels:
    • Environment:
      any

      Description

      Hi,

      I have to serialize the following class with C# driver:

      class Sample
      {
          [BsonId]
          public object id {get;set;}
          public string name {get;set}
          public Sample2 sample2 {get;set;}
      }
       
      class Sample2
      {
          [BsonIgnoreIfNull]
          public DateTime? DealDate { get; set; }
       
          [BsonIgnoreIfNull]
          public DateTime? FinishDate { get; set; }
      }

      but I want to ignore sample2 field if this subdoc has no fields (if both dates are null).

      For now I can use ShouldSerializesample2, but i would like to implement that with some attribute like [BsonIgnoreIfNoElements]. If json has complex structure with nested nullable elements in subdocs it will be best solution.

      It's an open question how often this situation arises. Maybe this attribute deserves implementation. What's your opinion?

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: