private object DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap) { try { var nominalType = memberMap.MemberType; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null && nominalType.IsInterface) { bsonReader.ReadNull(); return(null); } Type actualType; if (bsonType == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = memberMap.GetDiscriminatorConvention(); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); return(serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions)); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", memberMap.ClassMap.ClassType.FullName, ex.Message); throw new FileFormatException(message, ex); } }
private void DeserializeMember(BsonReader bsonReader, object obj, BsonMemberMap memberMap) { try { var nominalType = memberMap.MemberType; object value = null; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null && nominalType.IsInterface) { bsonReader.ReadNull(); goto setvalue; } else if (memberMap.MemberTypeIsBsonValue) { if (bsonType == BsonType.Document && IsCSharpNullRepresentation(bsonReader)) { // if IsCSharpNullRepresentation returns true it will have consumed the document representing C# null goto setvalue; } // handle BSON null for backward compatibility with existing data (new data would have _csharpnull) if (bsonType == BsonType.Null && (nominalType != typeof(BsonValue) && nominalType != typeof(BsonNull))) { bsonReader.ReadNull(); goto setvalue; } } Type actualType; if (bsonType == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = memberMap.GetDiscriminatorConvention(); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); value = serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions); setvalue: memberMap.Setter(obj, value); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", obj.GetType().FullName, ex.Message); throw new FileFormatException(message, ex); } }
private object DeserializeMemberValue(BsonReader bsonReader, BsonMemberMap memberMap) { try { var nominalType = memberMap.MemberType; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null && nominalType.IsInterface) { bsonReader.ReadNull(); return null; } Type actualType; if (bsonType == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = memberMap.GetDiscriminatorConvention(); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); return serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", memberMap.ClassMap.ClassType.FullName, ex.Message); throw new Exception(message, ex); } }
private void DeserializeMember(BsonReader bsonReader, object obj, BsonMemberMap memberMap) { try { var nominalType = memberMap.MemberType; Type actualType; if (bsonReader.GetCurrentBsonType() == BsonType.Null) { actualType = nominalType; } else { var discriminatorConvention = memberMap.GetDiscriminatorConvention(); actualType = discriminatorConvention.GetActualType(bsonReader, nominalType); // returns nominalType if no discriminator found } var serializer = memberMap.GetSerializer(actualType); var value = serializer.Deserialize(bsonReader, nominalType, actualType, memberMap.SerializationOptions); memberMap.Setter(obj, value); } catch (Exception ex) { var message = string.Format( "An error occurred while deserializing the {0} {1} of class {2}: {3}", // terminating period provided by nested message memberMap.MemberName, (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property", obj.GetType().FullName, ex.Message); throw new FileFormatException(message, ex); } }