/// <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 the class is specified as opt-in serialization only, members must have the JsonMember attribute
            if (objType.GetCustomAttributes(typeof(JsonOptInAttribute), true).Length != 0)
            {
#if NETFX_CORE
                var customAttrs = member.GetCustomAttributes(typeof(JsonMemberAttribute), true);
                System.Attribute[] customAttrsArr = customAttrs == null ? null : customAttrs.ToArray();
                if (customAttrs == null || customAttrsArr == null || customAttrsArr.Length == 0)
                {
#else
                if (member.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length == 0)
                {
#endif
                    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);
        }
Example #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);
        }
Example #3
0
        private bool IsIgnored(Type objType, MemberInfo member, object obj)
        {
            if (JsonIgnoreAttribute.IsJsonIgnore(member))
            {
                return(true);
            }
            string 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 (objType.GetCustomAttributes(typeof(JsonOptInAttribute), true).Length != 0 && member.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length == 0)
            {
                return(true);
            }
            if (this.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);
        }
Example #4
0
        /// <summary>
        /// Gets the name specified for use in Json serialization.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetJsonSpecifiedProperty(MemberInfo memberInfo)
        {
#if NETFX_CORE
            if (memberInfo == null)
            {
                return(null);
            }
            var attribute = memberInfo.GetCustomAttribute <JsonSpecifiedPropertyAttribute>(true);
            if (attribute == null)
            {
                return(null);
            }
#else
            if (memberInfo == null ||
                !Attribute.IsDefined(memberInfo, typeof(JsonSpecifiedPropertyAttribute)))
            {
                return(null);
            }
            JsonSpecifiedPropertyAttribute attribute = (JsonSpecifiedPropertyAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(JsonSpecifiedPropertyAttribute));
#endif
            return(attribute.SpecifiedProperty);
        }
Example #5
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))
            {
#if WINDOWS_STORE
                PropertyInfo specProp = TCU.GetTypeInfo(objType).GetRuntimeProperty(specifiedProperty);
#else
                PropertyInfo specProp = TCU.GetTypeInfo(objType).GetProperty(specifiedProperty);
#endif
                if (!PropertyInfo.Equals(specProp, null))
                {
                    object isSpecified = specProp.GetValue(obj, null);
                    if (isSpecified is Boolean && !Convert.ToBoolean(isSpecified))
                    {
                        return(true);
                    }
                }
            }

            //If the class is specified as opt-in serialization only, members must have the JsonMember attribute
            #if WINDOWS_STORE
            if (objType.GetCustomAttribute <JsonOptInAttribute>(true) != null)
            {
                if (member.GetCustomAttribute <JsonMemberAttribute>(true) == null)
                {
                    return(true);
                }
            }
            #else
            if (objType.GetCustomAttributes(typeof(JsonOptInAttribute), true).Length != 0)
            {
                if (member.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length == 0)
                {
                    return(true);
                }
            }
            #endif

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

#if WINDOWS_STORE
                PropertyInfo specProp = TCU.GetTypeInfo(objType).GetRuntimeProperty(specifiedProperty);
#else
                PropertyInfo specProp = TCU.GetTypeInfo(objType).GetProperty(member.Name + "Specified");
#endif

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

            return(false);
        }