I want to serialize Dictionary<long, VALUE>
to following JSON in MongoDB.
{
"213" : {},
"63624" : {},
...
}
I don't want other DictionaryRepresentation
except DictionaryRepresentation.Document.Document
I am using MongoDB C# Driver (v2.0.1.27), and it is not smart to convert the long
type key into string
, which causes an exception.
Thank you
You can do this with the existing serializers but it requires a small amount of configuration.
Assume the following class:
You can configure a custom serializer for the D property (the Dictionary) that uses a key serializer that serializes longs to strings. The code would look like this:
The key idea here is that even though the keys and values are both longs, we are using different serializers for the keys and the values.
If we then run a quick test:
We see that the Dictionary keys are now being serialized as strings:
Also I worked out another solution, hope it helps other people
Then on the field