Skip to content

Commit

Permalink
Add support for Dictionary serialization and deserialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed May 15, 2010
1 parent 6990f9b commit 38cc8ec
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 12 deletions.
Expand Up @@ -116,25 +116,43 @@ public void CanSetValueOnNullablPropertys()
Assert.AreEqual(10,obj.Value);
}

public class DictionaryProperty
public class GenericDictionary
{
public Dictionary<string, string> Test { get; set; }
public Dictionary<string, int> Property { get; set; }
}

[Test]
public void CanSerializeAndDeserializeDictionarys()
public void CanSerializeAndDeserializeGenericDictionarys()
{
var dict = new DictionaryProperty
{
Test = new Dictionary<string, string> {{"test", "test"}}
};
var bson = Serialize<DictionaryProperty>(dict);
var obj = new GenericDictionary{Property = new Dictionary<string, int> { { "key1", 10 }, { "key2", 20 } }};
var bson = Serialize<GenericDictionary>(obj);
var prop = Deserialize<GenericDictionary>(bson);

var prop = Deserialize<DictionaryProperty>(bson);
Assert.IsNotNull(prop);
Assert.IsNotNull(prop.Property);
Assert.AreEqual(2,prop.Property.Count);
Assert.Contains(new KeyValuePair<string, int>("key1", 10), prop.Property);
Assert.Contains(new KeyValuePair<string, int>("key2", 20), prop.Property);
}

public class HashSetHelper
{
public HashSet<string> Property { get; set; }
}

[Test]
public void CanSerializeAndDeserializeHashSet()
{
var obj = new HashSetHelper {Property = new HashSet<string> {"test1", "test2"}};
var bson = Serialize<HashSetHelper>(obj);
var prop = Deserialize<HashSetHelper>(bson);

Assert.IsNotNull(prop);
Assert.IsNotNull(prop.Test);
Assert.Contains(new KeyValuePair<string,string>("test","test"),prop.Test);
Assert.IsNotNull(prop.Property);
Assert.AreEqual(2, prop.Property.Count);

Assert.IsTrue(prop.Property.Contains("test1"));
Assert.IsTrue(prop.Property.Contains("test2"));
}
}
}
1 change: 1 addition & 0 deletions source/MongoDB/MongoDB.csproj
Expand Up @@ -132,6 +132,7 @@
<Compile Include="MapReduce.cs" />
<Compile Include="Commands\MapReduceCommand.cs" />
<Compile Include="Results\MapReduceResult.cs" />
<Compile Include="Serialization\Builders\DictionaryBuilder.cs" />
<Compile Include="Util\ScopedDictionary.cs" />
<Compile Include="Linq\Translators\AggregateChecker.cs" />
<Compile Include="Linq\Translators\AggregateRewriter.cs" />
Expand Down
6 changes: 5 additions & 1 deletion source/MongoDB/Serialization/BsonClassMapBuilder.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using MongoDB.Bson;
using MongoDB.Configuration.Mapping;
Expand Down Expand Up @@ -39,7 +40,10 @@ public object EndObject(object instance)
}

public object BeginArray()
{
{
if(typeof(IDictionary).IsAssignableFrom(_types.Peek()))
return new DictionaryBuilder(_types.Peek());

return new ArrayBuilder(_types.Peek());
}

Expand Down
67 changes: 67 additions & 0 deletions source/MongoDB/Serialization/Builders/DictionaryBuilder.cs
@@ -0,0 +1,67 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace MongoDB.Serialization.Builders
{
internal class DictionaryBuilder : IObjectBuilder
{
private readonly Type _elementType;
private readonly List<object> _elements;

/// <summary>
/// Initializes a new instance of the <see cref="DictionaryBuilder"/> class.
/// </summary>
/// <param name="elementType">Type of the element.</param>
public DictionaryBuilder(Type elementType)
{
if(elementType == null)
throw new ArgumentNullException("elementType");
_elementType = elementType;
_elements = new List<object>();
}

/// <summary>
/// Adds the property.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="value">The value.</param>
public void AddProperty(string name, object value)
{
_elements.Add(value);
}

/// <summary>
/// Builds the object.
/// </summary>
/// <returns></returns>
public object BuildObject()
{
var instance = (IDictionary)Activator.CreateInstance(_elementType);

foreach(KeyValueMapper keyValue in _elements)
instance.Add(keyValue.Key, keyValue.Value);

return instance;
}

/// <summary>
/// Gets the type of the property.
/// </summary>
/// <param name="name">The name.</param>
/// <returns></returns>
public Type GetPropertyType(string name)
{
return typeof(KeyValueMapper);
}

/// <summary>
///
/// </summary>
public class KeyValueMapper
{
public object Key { get; set; }
public object Value { get; set; }
}
}
}

0 comments on commit 38cc8ec

Please sign in to comment.