Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a test+solution for serializeing Enums as number when they are us…

…ed as Dictionary key.
  • Loading branch information...
commit b8a62c20a253e2b10f3e4bc82e619f0316103d7b 1 parent 97623f7
Steve Wagner lanwin authored
31 source/MongoDB.Tests/UnitTests/Serialization/SerializationFactoryTests.cs
View
@@ -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]);
+ }
}
}
4 source/MongoDB/Configuration/DictionaryAdapters/GenericDictionaryDictionaryAdapter.cs
View
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using MongoDB.Configuration.Mapping.Util;
namespace MongoDB.Configuration.DictionaryAdapters
{
@@ -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;
}
+
}
}
3  source/MongoDB/Configuration/DictionaryAdapters/GenericSortedListDictionaryAdapter.cs
View
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using MongoDB.Configuration.Mapping.Util;
namespace MongoDB.Configuration.DictionaryAdapters
{
@@ -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;
}
3  source/MongoDB/Configuration/DictionaryAdapters/HashtableDictionaryAdapter.cs
View
@@ -1,5 +1,6 @@
using System;
using System.Collections;
+using MongoDB.Configuration.Mapping.Util;
namespace MongoDB.Configuration.DictionaryAdapters
{
@@ -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;
}
11 source/MongoDB/Configuration/Mapping/Util/ValueConverter.cs
View
@@ -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();
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.