Exemplo n.º 1
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 (memberInfo == null ||
                !Attribute.IsDefined(memberInfo, typeof(JsonSpecifiedPropertyAttribute)))
            {
                return(null);
            }

            JsonSpecifiedPropertyAttribute attribute = (JsonSpecifiedPropertyAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(JsonSpecifiedPropertyAttribute));

            return(attribute.SpecifiedProperty);
        }
        /// <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 (member.GetCustomAttributes(typeof(JsonMemberAttribute), true).Length == 0)
                {
                    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);
        }