private void ProcessDefaultValue <T>(MemberInfo memberInfo, MemberMapping <T> memberMapping, JsonSerializerOptions options)
        {
            DefaultValueAttribute?defaultValueAttribute = memberInfo.GetCustomAttribute <DefaultValueAttribute>();

            if (defaultValueAttribute != null)
            {
                memberMapping.SetDefaultValue(defaultValueAttribute.Value);
            }

            if (memberInfo.IsDefined(typeof(JsonIgnoreIfDefaultAttribute)))
            {
                memberMapping.SetIgnoreIfDefault(true);
            }

#if NET5_0
            if (options.DefaultIgnoreCondition == JsonIgnoreCondition.WhenWritingDefault ||
                options.DefaultIgnoreCondition == JsonIgnoreCondition.WhenWritingNull &&
                (memberMapping.MemberType.IsClass || Nullable.GetUnderlyingType(memberMapping.MemberType) != null))
            {
                memberMapping.SetIgnoreIfDefault(true);
            }

            JsonIgnoreAttribute?jsonIgnoreAttribute = memberInfo.GetCustomAttribute <JsonIgnoreAttribute>();
            if (jsonIgnoreAttribute != null)
            {
                if (jsonIgnoreAttribute.Condition == JsonIgnoreCondition.WhenWritingDefault ||
                    jsonIgnoreAttribute.Condition == JsonIgnoreCondition.WhenWritingNull &&
                    (memberMapping.MemberType.IsClass || Nullable.GetUnderlyingType(memberMapping.MemberType) != null))
                {
                    memberMapping.SetIgnoreIfDefault(true);
                }
            }
#endif
        }
Ejemplo n.º 2
0
        private void ProcessDefaultValue <T>(MemberInfo memberInfo, MemberMapping <T> memberMapping)
        {
            DefaultValueAttribute?defaultValueAttribute = memberInfo.GetCustomAttribute <DefaultValueAttribute>();

            if (defaultValueAttribute != null)
            {
                memberMapping.SetDefaultValue(defaultValueAttribute.Value);
            }

            DataMemberAttribute?dataMemberAttribute = memberInfo.GetCustomAttribute <DataMemberAttribute>();

            if (dataMemberAttribute != null && !dataMemberAttribute.EmitDefaultValue)
            {
                memberMapping.SetIgnoreIfDefault(true);
            }
        }