Beispiel #1
0
        private void SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap memberMap)
        {
            var value = memberMap.Getter(obj);

            if (!memberMap.ShouldSerialize(obj, value))
            {
                return; // don't serialize member
            }

            bsonWriter.WriteName(memberMap.ElementName);
            var nominalType = memberMap.MemberType;

            if (value == null && nominalType.IsInterface)
            {
                bsonWriter.WriteNull();
            }
            else if (value == null && memberMap.MemberTypeIsBsonValue)
            {
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteBoolean("_csharpnull", true);
                bsonWriter.WriteEndDocument();
            }
            else
            {
                var actualType = (value == null) ? nominalType : value.GetType();
                var serializer = memberMap.GetSerializer(actualType);
                serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions);
            }
        }
        private void SerializeExtraElements(BsonSerializationContext context, object obj, BsonMemberMap extraElementsMemberMap)
        {
            var bsonWriter = context.Writer;

            var extraElements = extraElementsMemberMap.Getter(obj);

            if (extraElements != null)
            {
                if (extraElementsMemberMap.MemberType == typeof(BsonDocument))
                {
                    var bsonDocument = (BsonDocument)extraElements;
                    foreach (var element in bsonDocument)
                    {
                        bsonWriter.WriteName(element.Name);
                        BsonValueSerializer.Instance.Serialize(context, element.Value);
                    }
                }
                else
                {
                    var dictionary = (IDictionary <string, object>)extraElements;
                    foreach (var key in dictionary.Keys)
                    {
                        bsonWriter.WriteName(key);
                        var value     = dictionary[key];
                        var bsonValue = BsonTypeMapper.MapToBsonValue(value);
                        BsonValueSerializer.Instance.Serialize(context, bsonValue);
                    }
                }
            }
        }
Beispiel #3
0
        private void SerializeMember(
            BsonWriter bsonWriter,
            object obj,
            BsonMemberMap memberMap
            )
        {
            var value = memberMap.Getter(obj);

            if (value == null && memberMap.IgnoreIfNull)
            {
                return; // don't serialize null value
            }
            if (memberMap.HasDefaultValue && !memberMap.SerializeDefaultValue && object.Equals(value, memberMap.DefaultValue))
            {
                return; // don't serialize default value
            }
            if (!memberMap.ShouldSerializeMethod(obj))
            {
                return; // the ShouldSerializeMethod determined that the member shouldn't be serialized
            }

            bsonWriter.WriteName(memberMap.ElementName);
            var nominalType = memberMap.MemberType;
            var actualType  = (value == null) ? nominalType : value.GetType();
            var serializer  = memberMap.GetSerializer(actualType);

            serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions);
        }
Beispiel #4
0
        private void SerializeExtraElements(BsonWriter bsonWriter, object obj, BsonMemberMap extraElementsMemberMap)
        {
            var extraElements = extraElementsMemberMap.Getter(obj);

            if (extraElements != null)
            {
                if (extraElementsMemberMap.MemberType == typeof(BsonDocument))
                {
                    var bsonDocument = (BsonDocument)extraElements;
                    foreach (var element in bsonDocument)
                    {
                        element.WriteTo(bsonWriter);
                    }
                }
                else
                {
                    var dictionary = (IDictionary <string, object>)extraElements;
                    foreach (var key in dictionary.Keys)
                    {
                        bsonWriter.WriteName(key);
                        var value = dictionary[key];
                        if (value == null)
                        {
                            bsonWriter.WriteNull();
                        }
                        else
                        {
                            var bsonValue = BsonTypeMapper.MapToBsonValue(dictionary[key]);
                            bsonValue.WriteTo(bsonWriter);
                        }
                    }
                }
            }
        }
        private void SerializeExtraElements(BsonWriter bsonWriter, object obj, BsonMemberMap extraElementsMemberMap)
        {
            var extraElements = (BsonDocument)extraElementsMemberMap.Getter(obj);

            if (extraElements != null)
            {
                foreach (var element in extraElements)
                {
                    element.WriteTo(bsonWriter);
                }
            }
        }
        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;
        }
        private void SerializeNormalMember(BsonSerializationContext context, object obj, BsonMemberMap memberMap)
        {
            var bsonWriter = context.Writer;

            var value = memberMap.Getter(obj);

            if (!memberMap.ShouldSerialize(obj, value))
            {
                return; // don't serialize member
            }

            bsonWriter.WriteName(memberMap.ElementName);
            memberMap.GetSerializer().Serialize(context, value);
        }
Beispiel #9
0
        private void SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap memberMap)
        {
            var value = memberMap.Getter(obj);

            if (!memberMap.ShouldSerialize(obj, value))
            {
                return; // don't serialize member
            }

            bsonWriter.WriteName(memberMap.ElementName);
            var nominalType = memberMap.MemberType;
            var actualType  = (value == null) ? nominalType : value.GetType();
            var serializer  = memberMap.GetSerializer(actualType);

            serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions);
        }
 // 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 SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap memberMap)
		{
			object objA = memberMap.Getter(obj);
			if (objA == null && memberMap.IgnoreIfNull || (memberMap.HasDefaultValue && !memberMap.SerializeDefaultValue && object.Equals(objA, memberMap.DefaultValue) || !memberMap.ShouldSerializeMethod(obj)))
				return;
			bsonWriter.WriteName(memberMap.ElementName);
			Type memberType = memberMap.MemberType;
			Type actualType = objA == null ? memberType : objA.GetType();
			if (IsRelation(memberMap))
				ValidateVirtualRelation(memberMap);
			memberMap.GetSerializer(actualType).Serialize(bsonWriter, memberType, objA, memberMap.SerializationOptions);
		}
        private void SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap memberMap)
        {
            var value = memberMap.Getter(obj);

            if (!memberMap.ShouldSerialize(obj, value))
            {
                return; // don't serialize member
            }

            bsonWriter.WriteName(memberMap.ElementName);
            var nominalType = memberMap.MemberType;
            var actualType = (value == null) ? nominalType : value.GetType();
            var serializer = memberMap.GetSerializer(actualType);
            serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions);
        }
 private void SerializeExtraElements(BsonWriter bsonWriter, object obj, BsonMemberMap extraElementsMemberMap)
 {
     var extraElements = (BsonDocument)extraElementsMemberMap.Getter(obj);
     if (extraElements != null)
     {
         foreach (var element in extraElements)
         {
             element.WriteTo(bsonWriter);
         }
     }
 }
 // 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 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 SerializeMember(BsonWriter bsonWriter, object obj, BsonMemberMap memberMap)
        {
            var value = memberMap.Getter(obj);

            if (!memberMap.ShouldSerialize(obj, value))
            {
                return; // don't serialize member
            }

            bsonWriter.WriteName(memberMap.ElementName);
            var nominalType = memberMap.MemberType;
            if (value == null && nominalType.IsInterface)
            {
                bsonWriter.WriteNull();
            }
            else if (value == null && memberMap.MemberTypeIsBsonValue)
            {
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteBoolean("_csharpnull", true);
                bsonWriter.WriteEndDocument();
            }
            else
            {
                var actualType = (value == null) ? nominalType : value.GetType();
                var serializer = memberMap.GetSerializer(actualType);
                serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions);
            }
        }
		private void SerializeExtraElements(BsonWriter bsonWriter, object obj, BsonMemberMap extraElementsMemberMap)
		{
			BsonDocument bsonDocument = (BsonDocument)extraElementsMemberMap.Getter(obj);
			if (!(bsonDocument != (BsonDocument)null))
				return;
			foreach (BsonElement bsonElement in bsonDocument)
			{
				bsonWriter.WriteName(bsonElement.Name);
				bsonElement.Value.WriteTo(bsonWriter);
			}
		}
        private void SerializeMember(
            BsonWriter bsonWriter,
            object obj,
            BsonMemberMap memberMap
        ) {
            var value = memberMap.Getter(obj);
            if (value == null && memberMap.IgnoreIfNull) {
                return; // don't serialize null value
            }
            if (memberMap.HasDefaultValue && !memberMap.SerializeDefaultValue && object.Equals(value, memberMap.DefaultValue)) {
                return; // don't serialize default value
            }
            if (!memberMap.ShouldSerializeMethod(obj)) {
                return; // the ShouldSerializeMethod determined that the member shouldn't be serialized
            }

            bsonWriter.WriteName(memberMap.ElementName);
            var nominalType = memberMap.MemberType;
            var actualType = (value == null) ? nominalType : value.GetType();
            var serializer = memberMap.GetSerializer(actualType);
            serializer.Serialize(bsonWriter, nominalType, value, memberMap.SerializationOptions);
        }
 private void SerializeExtraElements(BsonWriter bsonWriter, object obj, BsonMemberMap extraElementsMemberMap)
 {
     var extraElements = extraElementsMemberMap.Getter(obj);
     if (extraElements != null)
     {
         if (extraElementsMemberMap.MemberType == typeof(BsonDocument))
         {
             var bsonDocument = (BsonDocument)extraElements;
             foreach (var element in bsonDocument)
             {
                 bsonWriter.WriteName(element.Name);
                 BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), element.Value, null);
             }
         }
         else
         {
             var dictionary = (IDictionary<string, object>)extraElements;
             foreach (var key in dictionary.Keys)
             {
                 bsonWriter.WriteName(key);
                 var value = dictionary[key];
                 if (value == null)
                 {
                     bsonWriter.WriteNull();
                 }
                 else
                 {
                     var bsonValue = BsonTypeMapper.MapToBsonValue(dictionary[key]);
                     BsonValueSerializer.Instance.Serialize(bsonWriter, typeof(BsonValue), bsonValue, null);
                 }
             }
         }
     }
 }
		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;
		}