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

Serialization of dictionary of dictionaries always fails

      When you serialize a dictionary with DictionaryRepresentation.Document the keys must be serialized as strings.

      If you have a dictionary with different keys you can use the BsonDictionaryOptionsAttribute to change the DictionaryRepresentation. However in a dictionary of dictionaries, you can't affect the inner dictionaries so the following class can never be serialized:

          public class Hamster
          {
              public ObjectId Id { get; private set; }
              public Dictionary<string, Dictionary<DateTime,string>> Dictionary { get; private set; }
              public Hamster()
              {
                  Id = ObjectId.GenerateNewId();
                  Dictionary = new Dictionary<string, Dictionary<DateTime, string>>();
                  Dictionary["hamster"] = new Dictionary<DateTime, string> {{DateTime.UtcNow, "hamster"}};
              }
          }
      

      I believe the issue is in BsonDictionaryOptionsAttribute.Apply where the DictionaryRepresentation is applied to the serializer or the child serializer (via base.Apply(serializer)) instead of to both (if it applies).

      The same is true for list of dictionaries and probably other collections.

            Assignee:
            Unassigned Unassigned
            Reporter:
            I3arnon Bar Arnon
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: