-
Type: Task
-
Resolution: Done
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.0
-
Component/s: BSON, Serialization
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.
- is related to
-
CSHARP-938 Write API Documentation and Review Visibility of Classes/Members
- Closed