Пример #1
0
        private void DetermineNumberHandling(JsonNumberHandling?parentTypeNumberHandling)
        {
            bool numberHandlingIsApplicable = ConverterBase.IsInternalConverterForNumberType || TypeIsCollectionOfNumbersWithInternalConverter();

            if (IsForClassInfo)
            {
                if (parentTypeNumberHandling != null && !ConverterBase.IsInternalConverter)
                {
                    ThrowHelper.ThrowInvalidOperationException_NumberHandlingOnPropertyInvalid(this);
                }

                if (numberHandlingIsApplicable)
                {
                    // This logic is to honor JsonNumberHandlingAttribute placed on
                    // custom collections e.g. public class MyNumberList : List<int>.

                    // Priority 1: Get handling from the type (parent type in this case is the type itself).
                    NumberHandling = parentTypeNumberHandling;

                    // Priority 2: Get handling from JsonSerializerOptions instance.
                    if (!NumberHandling.HasValue && Options.NumberHandling != JsonNumberHandling.Strict)
                    {
                        NumberHandling = Options.NumberHandling;
                    }
                }
            }
            else
            {
                Debug.Assert(MemberInfo != null);

                JsonNumberHandlingAttribute?attribute = GetAttribute <JsonNumberHandlingAttribute>(MemberInfo);
                if (attribute != null && !numberHandlingIsApplicable)
                {
                    ThrowHelper.ThrowInvalidOperationException_NumberHandlingOnPropertyInvalid(this);
                }

                if (numberHandlingIsApplicable)
                {
                    // Priority 1: Get handling from attribute on property or field.
                    JsonNumberHandling?handling = attribute?.Handling;

                    // Priority 2: Get handling from attribute on parent class type.
                    handling ??= parentTypeNumberHandling;

                    // Priority 3: Get handling from JsonSerializerOptions instance.
                    if (!handling.HasValue && Options.NumberHandling != JsonNumberHandling.Strict)
                    {
                        handling = Options.NumberHandling;
                    }

                    NumberHandling = handling;
                }
            }
        }
Пример #2
0
        private void DetermineNumberHandling(JsonNumberHandling?parentTypeNumberHandling)
        {
            if (IsForClassInfo)
            {
                if (parentTypeNumberHandling != null && !ConverterBase.IsInternalConverter)
                {
                    ThrowHelper.ThrowInvalidOperationException_NumberHandlingOnPropertyInvalid(this);
                }

                // Priority 1: Get handling from the type (parent type in this case is the type itself).
                NumberHandling = parentTypeNumberHandling;

                // Priority 2: Get handling from JsonSerializerOptions instance.
                if (!NumberHandling.HasValue && Options.NumberHandling != JsonNumberHandling.Strict)
                {
                    NumberHandling = Options.NumberHandling;
                }
            }
            else
            {
                JsonNumberHandling?handling = null;

                // Priority 1: Get handling from attribute on property or field.
                if (MemberInfo != null)
                {
                    JsonNumberHandlingAttribute?attribute = GetAttribute <JsonNumberHandlingAttribute>(MemberInfo);

                    if (attribute != null &&
                        !ConverterBase.IsInternalConverterForNumberType &&
                        ((ClassType.Enumerable | ClassType.Dictionary) & ClassType) == 0)
                    {
                        ThrowHelper.ThrowInvalidOperationException_NumberHandlingOnPropertyInvalid(this);
                    }

                    handling = attribute?.Handling;
                }

                // Priority 2: Get handling from attribute on parent class type.
                handling ??= parentTypeNumberHandling;

                // Priority 3: Get handling from JsonSerializerOptions instance.
                if (!handling.HasValue && Options.NumberHandling != JsonNumberHandling.Strict)
                {
                    handling = Options.NumberHandling;
                }

                NumberHandling = handling;
            }
        }