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

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

    • Type: Icon: Improvement Improvement
    • Resolution: Won't Do
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.4.4
    • Component/s: Serialization
    • Environment:
      Win10 VisualStudio 2017
    • None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      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:

      Unable to find source-code formatter for language: bson. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      {
          "_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:

      Unable to find source-code formatter for language: bson. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      {
          "_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,

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

              Created:
              Updated:
              Resolved: