Skip to content

Commit

Permalink
Allow to deserialize propertys with nullable values.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed May 12, 2010
1 parent 79eebe4 commit a4046e7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Expand Up @@ -87,5 +87,32 @@ public void CanNotSetPrivatePropertys()
Assert.IsNotNull(prop);
Assert.AreEqual(0, prop.GetProperty());
}

public class NullableProperty
{
public double? Value { get; set; }
}

[Test]
public void CanSetNullOnNullablPropertys()
{
var bson = Serialize(new Document("Value", null));

var obj = Deserialize<NullableProperty>(bson);

Assert.IsNotNull(obj);
Assert.IsNull(obj.Value);
}

[Test]
public void CanSetValueOnNullablPropertys()
{
var bson = Serialize(new Document("Value", 10));

var obj = Deserialize<NullableProperty>(bson);

Assert.IsNotNull(obj);
Assert.AreEqual(10,obj.Value);
}
}
}
7 changes: 7 additions & 0 deletions source/MongoDB/Configuration/Mapping/Model/MemberMapBase.cs
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace MongoDB.Configuration.Mapping.Model
{
Expand Down Expand Up @@ -73,6 +74,12 @@ public virtual void SetValue(object instance, object value)

if(_memberReturnType.IsEnum)
value = Enum.ToObject(_memberReturnType, value);
else if(_memberReturnType.IsGenericType &&
_memberReturnType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
if(value!=null)
value = Convert.ChangeType(value, _memberReturnType.GetGenericArguments().First());
}
else if(code != TypeCode.Object)
value = Convert.ChangeType(value, _memberReturnType);
}
Expand Down

0 comments on commit a4046e7

Please sign in to comment.