Esempio n. 1
0
 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);
     }
 }
Esempio n. 2
0
        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 void DeserializeExtraElementMember(
            BsonDeserializationContext context,
            object obj,
            string elementName,
            BsonMemberMap extraElementsMemberMap)
        {
            var bsonReader = context.Reader;

            if (extraElementsMemberMap.MemberType == typeof(BsonDocument))
            {
                var extraElements = (BsonDocument)extraElementsMemberMap.Getter(obj);
                if (extraElements == null)
                {
                    extraElements = new BsonDocument();
                    extraElementsMemberMap.Setter(obj, extraElements);
                }

                var bsonValue = BsonValueSerializer.Instance.Deserialize(context);
                extraElements[elementName] = bsonValue;
            }
            else
            {
                var extraElements = (IDictionary <string, object>)extraElementsMemberMap.Getter(obj);
                if (extraElements == null)
                {
                    if (extraElementsMemberMap.MemberType == typeof(IDictionary <string, object>))
                    {
                        extraElements = new Dictionary <string, object>();
                    }
                    else
                    {
                        extraElements = (IDictionary <string, object>)Activator.CreateInstance(extraElementsMemberMap.MemberType);
                    }
                    extraElementsMemberMap.Setter(obj, extraElements);
                }

                var bsonValue = BsonValueSerializer.Instance.Deserialize(context);
                extraElements[elementName] = BsonTypeMapper.MapToDotNetValue(bsonValue);
            }
        }
        // private methods
        private void DeserializeExtraElement(BsonReader bsonReader, object obj, string elementName, BsonMemberMap extraElementsMemberMap)
        {
            var extraElements = (BsonDocument)extraElementsMemberMap.Getter(obj);

            if (extraElements == null)
            {
                extraElements = new BsonDocument();
                extraElementsMemberMap.Setter(obj, extraElements);
            }
            var value = BsonValue.ReadFrom(bsonReader);

            extraElements[elementName] = value;
        }
Esempio n. 5
0
        private void DeserializeMember(
            BsonReader bsonReader,
            object obj,
            BsonMemberMap memberMap
            )
        {
            var  nominalType = memberMap.MemberType;
            Type actualType;

            if (bsonReader.CurrentBsonType == BsonType.Null)
            {
                actualType = nominalType;
            }
            else
            {
                var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType);
                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);
        }
 private void DeserializeMember(BsonReader bsonReader, object obj, BsonMemberMap memberMap)
 {
     try
     {
         var nominalType = memberMap.MemberType;
         Type actualType;
         if (bsonReader.CurrentBsonType == BsonType.Null)
         {
             actualType = nominalType;
         }
         else
         {
             var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType);
             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);
     }
 }
 // private methods
 private void DeserializeExtraElement(
     BsonReader bsonReader,
     object obj,
     string elementName,
     BsonMemberMap extraElementsMemberMap)
 {
     var extraElements = (BsonDocument)extraElementsMemberMap.Getter(obj);
     if (extraElements == null)
     {
         extraElements = new BsonDocument();
         extraElementsMemberMap.Setter(obj, extraElements);
     }
     var value = BsonValue.ReadFrom(bsonReader);
     extraElements[elementName] = value;
 }
        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 methods
 private void DeserializeExtraElement(
     BsonReader bsonReader,
     object obj,
     string elementName,
     BsonMemberMap extraElementsMemberMap)
 {
     if (extraElementsMemberMap.MemberType == typeof(BsonDocument))
     {
         var extraElements = (BsonDocument)extraElementsMemberMap.Getter(obj);
         if (extraElements == null)
         {
             extraElements = new BsonDocument();
             extraElementsMemberMap.Setter(obj, extraElements);
         }
         var bsonValue = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
         extraElements[elementName] = bsonValue;
     }
     else
     {
         var extraElements = (IDictionary<string, object>)extraElementsMemberMap.Getter(obj);
         if (extraElements == null)
         {
             if (extraElementsMemberMap.MemberType == typeof(IDictionary<string, object>))
             {
                 extraElements = new Dictionary<string, object>();
             }
             else
             {
                 extraElements = (IDictionary<string, object>)Activator.CreateInstance(extraElementsMemberMap.MemberType);
             }
             extraElementsMemberMap.Setter(obj, extraElements);
         }
         var bsonValue = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
         extraElements[elementName] = BsonTypeMapper.MapToDotNetValue(bsonValue);
     }
 }
 private void DeserializeMember(
     BsonReader bsonReader,
     object obj,
     BsonMemberMap memberMap
 ) {
     var nominalType = memberMap.MemberType;
     Type actualType;
     if (bsonReader.CurrentBsonType == BsonType.Null) {
         actualType = nominalType;
     } else {
         var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(nominalType);
         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);
 }
 private static IDictionary<string, object> EnsureExtraElements(object obj, BsonMemberMap extraElementsMemberMap)
 {
     // TODO: make this method protected non-static and pull up?
     var extraElements = (IDictionary<string, object>) extraElementsMemberMap.Getter(obj);
     if (extraElements == null)
     {
         if (extraElementsMemberMap.MemberType == typeof (IDictionary<string, object>))
         {
             extraElements = new Dictionary<string, object>();
         }
         else
         {
             extraElements = (IDictionary<string, object>) Activator.CreateInstance(extraElementsMemberMap.MemberType);
         }
         extraElementsMemberMap.Setter(obj, extraElements);
     }
     return extraElements;
 }
		private void DeserializeMember(BsonReader bsonReader, object obj, BsonMemberMap memberMap)
		{
			try
			{
				Type memberType = memberMap.MemberType;
				Type actualType = bsonReader.CurrentBsonType != BsonType.Null ? BsonDefaultSerializer.LookupDiscriminatorConvention(memberType).GetActualType(bsonReader, memberType) : memberType;
				object obj1 = memberMap.GetSerializer(actualType).Deserialize(bsonReader, memberType, actualType, memberMap.SerializationOptions);
				// --- Added
				if (IsRelation(memberMap))
				{
					ValidateVirtualRelation(memberMap);
					if (obj1 != null)
						if (IsReferencesManyRelation(memberMap))
							((IDocument)obj).ReferencesManyIDs[memberMap.MemberName] = ((IEnumerable)obj1).Cast<ObjectId>().ToList();
						else
							((IDocument) obj).ReferencesOneIDs[memberMap.MemberName] = (ObjectId) obj1;
				}
				// --- Added
				else
					memberMap.Setter(obj, obj1);
			}
			catch (Exception ex)
			{
				throw new FileFormatException(string.Format("An error occurred while deserializing the {0} {1} of class {2}: {3}", (object)memberMap.MemberName, memberMap.MemberInfo.MemberType == MemberTypes.Field ? (object)"field" : (object)"property", (object)obj.GetType().FullName, (object)ex.Message), ex);
			}
		}
		private void DeserializeExtraElement(BsonReader bsonReader, object obj, string elementName, BsonMemberMap extraElementsMemberMap)
		{
			BsonDocument bsonDocument = (BsonDocument)extraElementsMemberMap.Getter(obj);
			if (bsonDocument == (BsonDocument)null)
			{
				bsonDocument = new BsonDocument();
				extraElementsMemberMap.Setter(obj, (object)bsonDocument);
			}
			BsonValue bsonValue = BsonValue.ReadFrom(bsonReader);
			bsonDocument[elementName] = bsonValue;
		}