BsonSerializationException when serializing a Dictionary<DateTime,T> to BSON
Asked Answered
H

4

34

I've recently moved to the new MongoDB C# driver v2.0 from the deprecated v1.9.

Now, when I serialize a class that has a dictionary I sometimes run into the following BsonSerializationException:

MongoDB.Bson.BsonSerializationException: When using DictionaryRepresentation.Document key values must serialize as strings.

Here's a minimal reproduce:

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

static void Main()
{
    Console.WriteLine(new Hamster().ToJson());
}
Hellhound answered 23/1, 2015 at 14:16 Comment(0)
H
61

The problem is that the new driver serializes dictionaries as a document by default.

The MongoDB C# driver has 3 ways to serialize a dictionary: Document, ArrayOfArrays & ArrayOfDocuments (more on that in the docs). When it serializes as a document the dictionary keys are the names of the BSON element which has some limitations (for example, as the error suggests, they must be serialized as strings).

In this case, the dictionary's keys are DateTimes which aren't serialized as strings, but as Dates so we need to choose another DictionaryRepresentation.

To change the serialization of this specific property we can use the BsonDictionaryOptions attribute with a different DictionaryRepresentation:

[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfArrays)]
public Dictionary<DateTime, int> Dictionary { get; private set; }

However, we need to do that on every problematic member individually. To apply this DictionaryRepresentation to all the relevant members we can implement a a new convention:

class DictionaryRepresentationConvention : ConventionBase, IMemberMapConvention
{
    private readonly DictionaryRepresentation _dictionaryRepresentation;
    public DictionaryRepresentationConvention(DictionaryRepresentation dictionaryRepresentation)
    {
        _dictionaryRepresentation = dictionaryRepresentation;
    }
    public void Apply(BsonMemberMap memberMap)
    {
        memberMap.SetSerializer(ConfigureSerializer(memberMap.GetSerializer()));
    }
    private IBsonSerializer ConfigureSerializer(IBsonSerializer serializer)
    {
        var dictionaryRepresentationConfigurable = serializer as IDictionaryRepresentationConfigurable;
        if (dictionaryRepresentationConfigurable != null)
        {
            serializer = dictionaryRepresentationConfigurable.WithDictionaryRepresentation(_dictionaryRepresentation);
        }

        var childSerializerConfigurable = serializer as IChildSerializerConfigurable;
        return childSerializerConfigurable == null
            ? serializer
            : childSerializerConfigurable.WithChildSerializer(ConfigureSerializer(childSerializerConfigurable.ChildSerializer));
    }
} 

Which we register as follows:

ConventionRegistry.Register(
    "DictionaryRepresentationConvention",
    new ConventionPack {new DictionaryRepresentationConvention(DictionaryRepresentation.ArrayOfArrays)},
    _ => true);
Hellhound answered 23/1, 2015 at 14:16 Comment(2)
The configuration of the child serializer was causing some problems for me. I go through and register a bunch of serializers and this was causing a child serializer to be evaluated - normally this is lazy evaluated but this was causing the evaluation to be done earlier than before. The fact I hadn't registered the serializer meant that it registered a default one and then when I came to register mine it told me it had already been registered!Boaster
seems like a seriously bad move on their part given that there is no requirement for a dictionary key to be a string, i could understand is it was the default for a string dictionary with something else for every other type but to assume every dictionary is a string is awefulLotta
B
8

The answer above is great, but unfortunately triggers a StackOverflowException on certain recursive object hierarchies - here's a slightly improved and up to date version.

public class DictionaryRepresentationConvention : ConventionBase, IMemberMapConvention
{
    private readonly DictionaryRepresentation _dictionaryRepresentation;

    public DictionaryRepresentationConvention(DictionaryRepresentation dictionaryRepresentation = DictionaryRepresentation.ArrayOfDocuments)
    {
        // see http://mongodb.github.io/mongo-csharp-driver/2.2/reference/bson/mapping/#dictionary-serialization-options

        _dictionaryRepresentation = dictionaryRepresentation;
    }

    public void Apply(BsonMemberMap memberMap)
    {
        memberMap.SetSerializer(ConfigureSerializer(memberMap.GetSerializer(),Array.Empty<IBsonSerializer>()));
    }

    private IBsonSerializer ConfigureSerializer(IBsonSerializer serializer, IBsonSerializer[] stack)
    {
        if (serializer is IDictionaryRepresentationConfigurable dictionaryRepresentationConfigurable)
        {
            serializer = dictionaryRepresentationConfigurable.WithDictionaryRepresentation(_dictionaryRepresentation);
        }

        if (serializer is IChildSerializerConfigurable childSerializerConfigurable)
        {
            if (!stack.Contains(childSerializerConfigurable.ChildSerializer))
            {
                var newStack = stack.Union(new[] { serializer }).ToArray();
                var childConfigured = ConfigureSerializer(childSerializerConfigurable.ChildSerializer, newStack);
                return childSerializerConfigurable.WithChildSerializer(childConfigured);
            }
        }

        return serializer;
    }
Bortz answered 30/4, 2019 at 9:46 Comment(0)
P
5

If like me you just wanted to apply this for a single field in a class, I achieved that like this (thanks to the other answers):

BsonClassMap.RegisterClassMap<TestClass>(cm =>
{
    cm.AutoMap();
    var memberMap = cm.GetMemberMap(x => x.DictionaryField);
    var serializer = memberMap.GetSerializer();
    if (serializer is IDictionaryRepresentationConfigurable dictionaryRepresentationSerializer)
        serializer = dictionaryRepresentationSerializer.WithDictionaryRepresentation(DictionaryRepresentation.ArrayOfDocuments);
    memberMap.SetSerializer(serializer);
});

Or as an extention method:

BsonClassMap.RegisterClassMap<TestClass>(cm =>
{
    cm.AutoMap();
    cm.SetDictionaryRepresentation(x => x.DictionaryField, DictionaryRepresentation.ArrayOfDocuments);
});

public static class MapHelpers
{
    public static BsonClassMap<T> SetDictionaryRepresentation<T, TMember>(this BsonClassMap<T> classMap, Expression<Func<T,TMember>> memberLambda, DictionaryRepresentation representation)
    {
        var memberMap = classMap.GetMemberMap(memberLambda);
        var serializer = memberMap.GetSerializer();
        if (serializer is IDictionaryRepresentationConfigurable dictionaryRepresentationSerializer)
            serializer = dictionaryRepresentationSerializer.WithDictionaryRepresentation(representation);
        memberMap.SetSerializer(serializer);
        return classMap;
    }
}
Patti answered 18/9, 2019 at 10:5 Comment(0)
T
0

With above code, I get System.InvalidOperationException: 'ValueFactory attempted to access the Value property of this instance.'

Has something to do with a List<> property.

Trolly answered 8/7, 2022 at 20:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.