Exemplo n.º 1
0
        public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value, IEmitter context)
        {
            var configuration = this.handling;
            var yamlMember    = key.GetCustomAttribute <YamlMemberAttribute>();

            if (yamlMember != null && yamlMember.IsDefaultValuesHandlingSpecified)
            {
                configuration = yamlMember.DefaultValuesHandling;
            }

            switch (configuration)
            {
            case DefaultValuesHandling.OmitNull:
                if (value.Value is null)
                {
                    return(false);
                }
                break;

            case DefaultValuesHandling.OmitDefaults:
                var defaultValue = key.GetCustomAttribute <DefaultValueAttribute>()?.Value ?? GetDefault(key.Type);
                if (Equals(value.Value, defaultValue))
                {
                    return(false);
                }
                break;
            }

            return(base.EnterMapping(key, value, context));
        }
        public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value)
        {
            DefaultValueAttribute customAttribute = key.GetCustomAttribute <DefaultValueAttribute>();
            object y = (customAttribute == null) ? GetDefault(key.Type) : customAttribute.Value;

            return(!_objectComparer.Equals(value.Value, y) && base.EnterMapping(key, value));
        }
        public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value, IEmitter context)
        {
            var configuration = handling;
            var yamlMember    = key.GetCustomAttribute <YamlMemberAttribute>();

            if (yamlMember != null && yamlMember.IsDefaultValuesHandlingSpecified)
            {
                configuration = yamlMember.DefaultValuesHandling;
            }

            if ((configuration & DefaultValuesHandling.OmitNull) != 0)
            {
                if (value.Value is null)
                {
                    return(false);
                }
            }

            if ((configuration & DefaultValuesHandling.OmitEmptyCollections) != 0)
            {
                if (value.Value is IEnumerable enumerable)
                {
                    var enumerator  = enumerable.GetEnumerator();
                    var canMoveNext = enumerator.MoveNext();
                    if (enumerator is IDisposable disposable)
                    {
                        disposable.Dispose();
                    }

                    if (!canMoveNext)
                    {
                        return(false);
                    }
                }
            }

            if ((configuration & DefaultValuesHandling.OmitDefaults) != 0)
            {
                var defaultValue = key.GetCustomAttribute <DefaultValueAttribute>()?.Value ?? GetDefault(key.Type);
                if (Equals(value.Value, defaultValue))
                {
                    return(false);
                }
            }

            return(base.EnterMapping(key, value, context));
        }
Exemplo n.º 4
0
        private static IPropertyDescriptor DescriptorWithSerializablePropertyAttribute(SerializablePropertyAttribute attribute)
        {
            IPropertyDescriptor result = Substitute.For <IPropertyDescriptor>();

            result.GetCustomAttribute <SerializablePropertyAttribute>().Returns(attribute);

            return(result);
        }
Exemplo n.º 5
0
        public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value)
        {
            var defaultValueAttribute = key.GetCustomAttribute <DefaultValueAttribute>();
            var defaultValue          = defaultValueAttribute != null
                                ? defaultValueAttribute.Value
                                : GetDefault(key.Type);

            return(!_objectComparer.Equals(value.Value, defaultValue) &&
                   base.EnterMapping(key, value));
        }
		public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value)
		{
			var defaultValueAttribute = key.GetCustomAttribute<DefaultValueAttribute>();
			var defaultValue = defaultValueAttribute != null
				? defaultValueAttribute.Value
				: GetDefault(key.Type);

			return !_objectComparer.Equals(value.Value, defaultValue)
				   && base.EnterMapping(key, value);
		}
        public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value, IEmitter context)
        {
            var yamlMember = key.GetCustomAttribute <YamlMemberAttribute>();

            if (yamlMember?.Description != null)
            {
                context.Emit(new Core.Events.Comment(yamlMember.Description, false));
            }

            return(base.EnterMapping(key, value, context));
        }
        public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value, IEmitter context)
        {
            var defaultValueAttribute = key.GetCustomAttribute <DefaultValueAttribute>();

            var defaultValue = defaultValueAttribute != null
                ? defaultValueAttribute.Value
                : GetDefault(key.Type);

            return(!Equals(value.Value, defaultValue) &&
                   !IsEmptyArray(value.Type, value.Value) &&
                   base.EnterMapping(key, value, context));
        }
Exemplo n.º 9
0
        public override bool EnterMapping(IPropertyDescriptor key, IObjectDescriptor value, IEmitter context)
        {
            var defaultValueAttribute = key.GetCustomAttribute <DefaultValueAttribute>();
            var defaultValue          = defaultValueAttribute != null ? defaultValueAttribute.Value : GetDefault(key.Type);

            if (ObjectComparer.Equals(value.Value, defaultValue))
            {
                return(false);
            }

            return(base.EnterMapping(key, value, context));
        }
Exemplo n.º 10
0
            public T GetCustomAttribute <T>() where T : Attribute
            {
                // Check for a Property
                var prop = classType.GetPublicProperty(Name);

                if (prop != null)
                {
                    var attr = overrides.GetAttribute <T>(prop.DeclaringType, Name);
                    if (attr != null)
                    {
                        return(attr);
                    }
                }

                // Default to base behavior
                return(baseDescriptor.GetCustomAttribute <T>());
            }
Exemplo n.º 11
0
        protected static T Get(Type attributeType, IPropertyDescriptor descriptor)
        {
            if (attributeType == null)
            {
                return(null);
            }
            var instance = descriptor.GetCustomAttribute(attributeType);

            if (instance == null)
            {
                return(null);
            }
            var attr = new T();

            attr.Instance = instance;
            return(attr);
        }
Exemplo n.º 12
0
            private IPropertyDescriptor TrimPropertySuffix(IPropertyDescriptor pd, Type type)
            {
                if (!pd.Name.EndsWith("Property"))
                {
                    return(pd);
                }

                // This might have been renamed by AutoRest.  See if there is a
                // JsonPropertyAttribute.PropertyName and use that instead if there is.
                var jpa = pd.GetCustomAttribute <JsonPropertyAttribute>();

                if (jpa == null || String.IsNullOrEmpty(jpa.PropertyName))
                {
                    return(pd);
                }

                return(new RenamedPropertyDescriptor(pd, jpa.PropertyName));
            }
Exemplo n.º 13
0
        private static IPropertyDescriptor ConvertPropertyDescriptor(IPropertyDescriptor propertyDescriptor)
        {
            SerializablePropertyAttribute attribute = propertyDescriptor.GetCustomAttribute <SerializablePropertyAttribute>();

            if (attribute is null)
            {
                return(null);
            }

            PropertyDescriptor result = new PropertyDescriptor(propertyDescriptor);

            if (attribute.Name != null)
            {
                result.Name = attribute.Name;
            }

            result.Order = attribute.BinaryOrder;

            return(result);
        }
Exemplo n.º 14
0
 public T GetCustomAttribute <T>() where T : Attribute
 {
     return(baseDescriptor.GetCustomAttribute <T>());
 }
Exemplo n.º 15
0
            public T GetCustomAttribute <T>() where T : Attribute
            {
                var attr = overrides.GetAttribute <T>(classType, Name);

                return(attr ?? baseDescriptor.GetCustomAttribute <T>());
            }