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

BsonExtraElements Dictionary<string, Object> fails serialization for complex types

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Minor - P4 Minor - P4
    • None
    • 2.4.4
    • Serialization
    • Win10 VisualStudio 2017

    Description

      Having the following situation:

      public class Article
      {
          [BsonId]
          [BsonRepresentation(BsonType.ObjectId)]
          public string Id { get; set; }
       
          [BsonExtraElements()]
          public Dictionary<string, Object> OtherData { get; set; }
      }
      

      It appears the the [BsonExtraElements] tag prevents the driver from serializing complex types. An example would be the following class:

      public class Bird
      {
          [BsonElement("_n")]
          [BsonRequired]
          public string Name { get; set; }
          [BsonElement("_s")]
          [BsonRequired]
          public string Species { get; set; }
          [BsonElement("_a")]
          [BsonRequired]
          public int Age { get; set; }
      }
      

      With this tag placed on the dictionary, the driver fails to serialize an Article, if it's dictionary contains a Brid, for example. Is this intended behavior?

      Without the tag it produces:

      {
          "_id" : ObjectId("59b011975b8c05376c319f01"),
          "OtherData" : {
              "testBird" : {
                  "_t" : "My.NameSpace.Bird",
                  "_n" : "Jerry",
                  "_s" : "European starling",
                  "_a" : 4
              }
          }
      }
      

      I would assume it would be possible with the tag to serialize this as:

      {
          "_id" : ObjectId("59b00cdd2b6e923b50fec052")
          "Bird" : {
              "_t"  : "My.NameSpace.Bird"
              "_n" : "Jerry",
              "_s" : "European starling",
              "_a" : 4
          }
      }
      

      So that later this could be deserialized back into the 'OtherData' dictionary with the correct type. So the question I'd like to ask if I'm doing something wrong? Or is it a bug? Or is it intended behavior?

      Greetings,

      Attachments

        Activity

          People

            Unassigned Unassigned
            bdr B. De Rijcke
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: