diff --git a/Serialiser_Engine/Compute/Deserialise/IObject.cs b/Serialiser_Engine/Compute/Deserialise/IObject.cs index 918b2dfff..92eb1e592 100644 --- a/Serialiser_Engine/Compute/Deserialise/IObject.cs +++ b/Serialiser_Engine/Compute/Deserialise/IObject.cs @@ -117,7 +117,7 @@ private static IObject SetProperties(this BsonDocument doc, Type type, IObject v { object propertyValue = item.Value.IDeserialise(prop.PropertyType, prop.GetValue(value), version, isUpgraded); - if (CanSetValueToProperty(prop.PropertyType, propertyValue)) + if (CanSetValueToProperty(prop.PropertyType, propertyValue, item.Value)) { prop.SetValue(value, propertyValue); } @@ -152,10 +152,10 @@ private static IObject SetProperties(this BsonDocument doc, Type type, IObject v /*******************************************/ - private static bool CanSetValueToProperty(Type propType, object value) + private static bool CanSetValueToProperty(Type propType, object value, BsonValue bsonValue) { if (value == null) - return !propType.IsValueType || Nullable.GetUnderlyingType(propType) != null; + return (!propType.IsValueType || Nullable.GetUnderlyingType(propType) != null) && (bsonValue == null || bsonValue.IsBsonNull); else return propType.IsAssignableFrom(value.GetType()); } diff --git a/Serialiser_Engine/Compute/Deserialise/Immutable.cs b/Serialiser_Engine/Compute/Deserialise/Immutable.cs index f3b77012d..0708a62a4 100644 --- a/Serialiser_Engine/Compute/Deserialise/Immutable.cs +++ b/Serialiser_Engine/Compute/Deserialise/Immutable.cs @@ -70,8 +70,10 @@ private static IObject DeserialiseImmutable(this BsonValue bson, Type targetType List arguments = new List(); foreach (var match in matches) { - object propertyValue = IDeserialise(match.Properties.First().Value, match.Parameter.ParameterType, null, version, isUpgraded); - if (CanSetValueToProperty(match.Parameter.ParameterType, propertyValue)) + BsonValue bsonValue = match.Properties.First().Value; + object propertyValue = IDeserialise(bsonValue, match.Parameter.ParameterType, null, version, isUpgraded); + + if (CanSetValueToProperty(match.Parameter.ParameterType, propertyValue, bsonValue)) arguments.Add(propertyValue); else {