[CSHARP-624] MongoDB.Bson.Serialization.Serializers.DictionarySerializer do not support empty string as key. Created: 04/Nov/12  Updated: 10/Dec/12  Resolved: 10/Dec/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.6.1, 1.7
Fix Version/s: 1.8

Type: Bug Priority: Minor - P4
Reporter: Michael Bertelsen Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

In C# it is ok create the following code:

var dic = new Dictionary<string,int> {

{"", 1}

, {"test", 2}};

but this dictionary can't get serialized when it has an empty string as key.

See: https://github.com/mongodb/mongo-csharp-driver/blob/master/Bson/Serialization/Serializers/DictionarySerializer.cs#L178



 Comments   
Comment by auto [ 10/Dec/12 ]

Author:

{u'date': u'2012-12-10T02:23:05Z', u'email': u'robert@10gen.com', u'name': u'rstam'}

Message: CSHARP-624: Handle empty string key value when serializing dictionaries.
Branch: x2.0
https://github.com/mongodb/mongo-csharp-driver/commit/b5c0cd59589468ccd3170ffb2b2fcbd9429339bd

Comment by auto [ 10/Dec/12 ]

Author:

{u'date': u'2012-12-10T02:23:05Z', u'email': u'robert@10gen.com', u'name': u'rstam'}

Message: CSHARP-624: Handle empty string key value when serializing dictionaries.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/585abea544882cbd7e57156fd3b898f23fe9364a

Comment by Robert Stam [ 07/Dec/12 ]

While empty field names may not be recommended, they are certainly legal, both in MongoDB and in Javascript.

They are definitely legal as the key value in a C# dictionary, so we must support serializing them somehow. Currently the DictionarySerializer<TKey, TValue> is throwing an IndexOutOfRangeException because the code is assuming the key is non-empty. That's a bug.

The DictionarySerializer already has a fallback representation for dictionaries that contain keys that aren't valid JSON field names, so the only question in my mind is whether an empty string key value should be represented using an empty field name or trigger the alternate representation for dictionaries.

The two possible representations look like this (where the Values field is a serialized Dictionary<string, int>):

{ "_id" : 1, "Values" : { "" : 1, "test" : 2 } }

and

{ "_id" : 1, "Values" : [["", 1], ["test", 2]] }

Comment by Craig Wilson [ 12/Nov/12 ]

So, while empty keys are technically supported by mongodb, it is highly advisable that they not be used. Is there are compelling reason you need this feature?

Generated at Wed Feb 07 21:37:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.