コード例 #1
0
        /// <summary>
        /// Gets a delegate which determines if the property or field should not be serialized based upon its value.
        /// </summary>
        /// <param name="member"></param>
        /// <returns>if has a value equivalent to the DefaultValueAttribute</returns>
        /// <remarks>
        /// This is useful for excluding serialization of default values.
        /// </remarks>
        public override ValueIgnoredDelegate GetValueIgnoredCallback(MemberInfo member)
        {
            Type objType = member.ReflectedType ?? member.DeclaringType;
            JsonSpecifiedPropertyAttribute specifiedPropertyAttr = TypeCoercionUtility.GetAttribute <JsonSpecifiedPropertyAttribute>(member);

            // look up specified property to see if exists
            GetterDelegate specifiedPropertyGetter = null;

            if (specifiedPropertyAttr != null && !String.IsNullOrEmpty(specifiedPropertyAttr.SpecifiedProperty))
            {
                PropertyInfo specProp = objType.GetProperty(specifiedPropertyAttr.SpecifiedProperty, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

                // ensure is correct return type
                if (specProp != null && specProp.PropertyType == typeof(bool))
                {
                    specifiedPropertyGetter = DynamicMethodGenerator.GetPropertyGetter(specProp);
                }
            }

            DefaultValueAttribute defaultAttr = TypeCoercionUtility.GetAttribute <DefaultValueAttribute>(member);

            if (defaultAttr == null)
            {
                if (specifiedPropertyGetter == null)
                {
                    // no need to even create a delegate
                    return(null);
                }

                // create a delegate which simply calls the specified property
                return(delegate(object target, object value)
                {
                    return Object.Equals(false, specifiedPropertyGetter(target));
                });
            }

            // extract default value since cannot change (is constant in attribute)
            object defaultValue = defaultAttr.Value;

            if (specifiedPropertyGetter == null)
            {
                // create a specific delegate which only has to compare the default value to the current value
                return(delegate(object target, object value)
                {
                    return Object.Equals(defaultValue, value);
                });
            }

            // create a combined delegate which checks both states
            return(delegate(object target, object value)
            {
                return
                Object.Equals(defaultValue, value) ||
                Object.Equals(false, specifiedPropertyGetter(target));
            });
        }
コード例 #2
0
    public static string GetJsonSpecifiedProperty(MemberInfo memberInfo)
    {
        if (memberInfo == null || !Attribute.IsDefined(memberInfo, typeof(JsonSpecifiedPropertyAttribute)))
        {
            return(null);
        }
        JsonSpecifiedPropertyAttribute jsonSpecifiedPropertyAttribute = (JsonSpecifiedPropertyAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(JsonSpecifiedPropertyAttribute));

        return(jsonSpecifiedPropertyAttribute.SpecifiedProperty);
    }
コード例 #3
0
            /// <summary>
            /// Determines if the property or field should not be serialized.
            /// </summary>
            /// <param name="objType"></param>
            /// <param name="member"></param>
            /// <param name="value"></param>
            /// <returns></returns>
            /// <remarks>
            /// Checks these in order, if any returns true then this is true:
            /// - is flagged with the JsonIgnoreAttribute property
            /// - has a JsonSpecifiedProperty which returns false
            /// </remarks>
            private bool IsIgnored(Type objType, MemberInfo member, object obj)
            {
                if (JsonIgnoreAttribute.IsJsonIgnore(member))
                {
                    return(true);
                }

                string specifiedProperty = JsonSpecifiedPropertyAttribute.GetJsonSpecifiedProperty(member);

                if (!String.IsNullOrEmpty(specifiedProperty))
                {
                    PropertyInfo specProp = objType.GetProperty(specifiedProperty);
                    if (specProp != null)
                    {
                        object isSpecified = specProp.GetValue(obj, null);
                        if (isSpecified is Boolean && !Convert.ToBoolean(isSpecified))
                        {
                            return(true);
                        }
                    }
                }

                if (this.settings.UseXmlSerializationAttributes)
                {
                    if (JsonIgnoreAttribute.IsXmlIgnore(member))
                    {
                        return(true);
                    }

                    PropertyInfo specProp = objType.GetProperty(member.Name + "Specified");
                    if (specProp != null)
                    {
                        object isSpecified = specProp.GetValue(obj, null);
                        if (isSpecified is Boolean && !Convert.ToBoolean(isSpecified))
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            }
コード例 #4
0
        private bool IsIgnored(Type objType, MemberInfo member, object obj)
        {
            if (JsonIgnoreAttribute.IsJsonIgnore(member))
            {
                return(true);
            }

            var jsonSpecifiedProperty = JsonSpecifiedPropertyAttribute.GetJsonSpecifiedProperty(member);

            if (!string.IsNullOrEmpty(jsonSpecifiedProperty))
            {
                PropertyInfo property = objType.GetProperty(jsonSpecifiedProperty);
                if (property != null)
                {
                    object value = property.GetValue(obj, null);
                    if (value is bool && !Convert.ToBoolean(value))
                    {
                        return(true);
                    }
                }
            }
            if (Settings.UseXmlSerializationAttributes)
            {
                if (JsonIgnoreAttribute.IsXmlIgnore(member))
                {
                    return(true);
                }
                PropertyInfo property2 = objType.GetProperty(member.Name + "Specified");
                if (property2 != null)
                {
                    object value2 = property2.GetValue(obj, null);
                    if (value2 is bool && !Convert.ToBoolean(value2))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }