Skip to content

Commit

Permalink
Add a test+solution for serializeing Enums as number when they are us…
Browse files Browse the repository at this point in the history
…ed as Dictionary key.
  • Loading branch information
lanwin committed Jul 14, 2010
1 parent 97623f7 commit b8a62c2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
Expand Up @@ -314,5 +314,36 @@ public void CanReadEmbeddedDocument()
Assert.AreEqual(1, embedded.Count);
Assert.AreEqual(10, embedded["value"]);
}

public class DictionaryWithEnumAsKeyHelper
{
public Dictionary<DateTimeKind, int> Dict { get; set; }
}

[Test]
public void SerializesAnEnumAsIntWhenItsUsedAsDictionaryKey()
{
var obj = new DictionaryWithEnumAsKeyHelper { Dict = new Dictionary<DateTimeKind, int> { { DateTimeKind.Utc, 9 } } };
var bson = Serialize<DictionaryWithEnumAsKeyHelper>(obj);
var doc = Deserialize<Document>(bson);

Assert.IsNotNull(doc);
var dict = doc["Dict"] as Document;
Assert.IsNotNull(dict);
Assert.AreEqual(1, dict.Count);
Assert.AreEqual(9, dict[Convert.ToString((int)DateTimeKind.Utc)]);
}

[Test]
public void CanDeserializeADictionaryWithEnumAsKey()
{
var bson = Serialize<Document>(new Document("Dict", new Dictionary<DateTimeKind,int> {{DateTimeKind.Utc,9}}));
var prop = Deserialize<DictionaryWithEnumAsKeyHelper>(bson);

Assert.IsNotNull(prop);
Assert.IsNotNull(prop.Dict);
Assert.AreEqual(1,prop.Dict.Count);
Assert.AreEqual(9,prop.Dict[DateTimeKind.Utc]);
}
}
}
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Configuration.Mapping.Util;

namespace MongoDB.Configuration.DictionaryAdapters
{
Expand Down Expand Up @@ -52,9 +53,10 @@ public Document GetDocument(object dictionary)
var doc = new Document();

foreach (var e in instance)
doc.Add(e.Key.ToString(), e.Value);
doc.Add(ValueConverter.ConvertKey(e.Key), e.Value);

return doc;
}

}
}
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using MongoDB.Configuration.Mapping.Util;

namespace MongoDB.Configuration.DictionaryAdapters
{
Expand Down Expand Up @@ -56,7 +57,7 @@ public Document GetDocument(object dictionary)
var doc = new Document();

foreach (var e in instance)
doc.Add(e.Key.ToString(), e.Value);
doc.Add(ValueConverter.ConvertKey(e.Key), e.Value);

return doc;
}
Expand Down
@@ -1,5 +1,6 @@
using System;
using System.Collections;
using MongoDB.Configuration.Mapping.Util;

namespace MongoDB.Configuration.DictionaryAdapters
{
Expand Down Expand Up @@ -54,7 +55,7 @@ public Document GetDocument(object collection)

var doc = new Document();
foreach (DictionaryEntry entry in hashtable)
doc.Add(entry.Key.ToString(), entry.Value);
doc.Add(ValueConverter.ConvertKey(entry.Key), entry.Value);

return doc;
}
Expand Down
11 changes: 11 additions & 0 deletions source/MongoDB/Configuration/Mapping/Util/ValueConverter.cs
Expand Up @@ -47,5 +47,16 @@ public static Array ConvertArray(object[] elements, Type type)

return array;
}

public static string ConvertKey(object key)
{
if(key == null)
throw new ArgumentNullException("key");

if(key is Enum)
return System.Convert.ToInt64(key).ToString();

return key.ToString();
}
}
}

0 comments on commit b8a62c2

Please sign in to comment.