[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. |
| 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: | ||
| 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: | ||
| 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>):
and
| ||
| 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? |