Browse Source

Incorporated Brian Knight's fix for CSHARP-310 with minor changes.

pull/71/head
rstam 14 years ago
parent
commit
bb88a48b01
  1. 2
      Bson/Serialization/BsonClassMap.cs
  2. 9
      Bson/Serialization/BsonMemberMap.cs
  3. 11
      BsonUnitTests/Jira/CSharp310Tests.cs

2
Bson/Serialization/BsonClassMap.cs

@ -878,7 +878,7 @@ namespace MongoDB.Bson.Serialization {
var defaultValue = conventions.DefaultValueConvention.GetDefaultValue(memberInfo);
if (defaultValue != null) {
memberMap.SetDefaultValue(defaultValue, memberMap.SerializeDefaultValue);
memberMap.SetDefaultValue(defaultValue);
}
// see if the class has a method called ShouldSerializeXyz where Xyz is the name of this member

9
Bson/Serialization/BsonMemberMap.cs

@ -242,7 +242,9 @@ namespace MongoDB.Bson.Serialization {
public BsonMemberMap SetDefaultValue(
object defaultValue
) {
return SetDefaultValue(defaultValue, true); // serializeDefaultValue
this.defaultValue = defaultValue;
this.hasDefaultValue = true;
return this;
}
/// <summary>
@ -255,9 +257,8 @@ namespace MongoDB.Bson.Serialization {
object defaultValue,
bool serializeDefaultValue
) {
this.hasDefaultValue = true;
this.serializeDefaultValue = serializeDefaultValue;
this.defaultValue = defaultValue;
SetDefaultValue(defaultValue);
SetSerializeDefaultValue(serializeDefaultValue);
return this;
}

11
BsonUnitTests/Jira/CSharp310Tests.cs

@ -24,23 +24,20 @@ using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
namespace MongoDB.BsonUnitTests.Jira {
[TestFixture]
public class CSharp310Tests {
private class C {
public int Id;
public Guid G;
public Guid G = Guid.Empty;
}
private class EmptyGuidDefaultValueConvention : IDefaultValueConvention {
public object GetDefaultValue(MemberInfo memberInfo) {
var type = (memberInfo.MemberType == MemberTypes.Field)
? ((FieldInfo)memberInfo).FieldType : ((PropertyInfo)memberInfo).PropertyType;
var type = (memberInfo.MemberType == MemberTypes.Field) ? ((FieldInfo) memberInfo).FieldType : ((PropertyInfo) memberInfo).PropertyType;
if (type == typeof(Guid)) {
return Guid.Empty;
}
else {
} else {
return null;
}
}
@ -67,4 +64,4 @@ namespace MongoDB.BsonUnitTests.Jira {
Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson<object>()));
}
}
}
}
Loading…
Cancel
Save