3

I am attempting to serialize a dictionary of dictionaries where the parent dictionary has its keys as type enum and child dictionary has keys of type DateTime. While attempting to insert into my collection I am met with

When using DictionaryRepresentation.Document key values must serialize as strings

I have read forums discussing serialization of enum into string, however with the model definitions that are currently in place I'm not sure how to go about with this method.

The two dictionary models that are currently in use are simply implementations of the Dictionary class:

IndexValues

{
    public class IndexValues : Dictionary<Index, DateDictionary> { }
}

DateDictionary

public class DateDictionary : Dictionary<DateTime, double>
    {
        public double AsOf(DateTime d)
        {
            DateTime date = d;
            while (date >= Keys.Min())
            {
                if (TryGetValue(date, out var value))
                {
                    return value;
                }
                date = date.AddDays(-1);
            }

            throw new Exception($"The dictionary doesn't have a value for any date less than or equal to {d}.");
        }
    }

Index

public enum Index
    {
        SPX,
        NDX
    }

I am adding values to the dictionary in my main program by simply instantiating new instances of both classes and adding the values in the required types.

IndexValues indexRecords = new IndexValues();

...

var enumHead = (Index)Enum.Parse(typeof(Index), header[l]); // header is simply a list of strings

...

DateDictionary dateDict = new DateDictionary();

var date = Convert.ToDateTime(dataLine[j]); // dataLine is simply a list of strings
var value = Convert.ToDouble(dataLine[k]);

if (indexRecords.ContainsKey(enumHead))
    {
        indexRecords[enumHead].Add(date, value);
    }
    else
    {
        dateDict.Add(date, value);
        indexRecords.Add(enumHead, dateDict);
    }

I have attempted defining the key and value within the model definitions and using both [BsonRepresentation(BsonType.String)] for the enum and DateTime values and [BsonDictionaryOptions(DictionaryRepresentation.Document)] for the DateDictionary, but am still met with the same issue.

What am I missing in this instance, and what would be the correct direction to look towards? For reference, I am using the C# driver v2.8.1.

1 Answer 1

6

It turns out I was needing two serializers instead of one. I defined these globally and I was able to insert without an issue.

BsonSerializer.RegisterSerializer(new EnumSerializer<Index>(BsonType.String));
BsonSerializer.RegisterSerializer(new DateTimeSerializer(DateTimeKind.Local, BsonType.String));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.