Exemplo n.º 1
0
        object ConvertValue(object value)
        {
            if (value == null)
            {
                return(null);
            }

            if (value is UInt64)
            {
                var number = (ulong)value;
                value = number <= Int64.MaxValue ? (long)number : (decimal)number;
            }

            if (value is Boolean)
            {
                if (_details.SupportsBoolean)
                {
                    return(value);
                }
                value = (bool)value ? 1 : 0;
            }
            else if (value is Decimal)
            {
                if (_details.SupportsDecimal)
                {
                    return(value);
                }
                value = Convert.ToString(value, CultureInfo.InvariantCulture);
            }

            if (value is Int32 || value is Int64 || value is Byte || value is SByte || value is Int16 || value is UInt16 || value is UInt32 || value is Enum)
            {
                return(_details.ConvertLongValue(Convert.ToInt64(value)));
            }

            if (value is Double || value is Single)
            {
                var number = Convert.ToDouble(value);
                if (RecognizeIntegers && number.IsSafeInteger())
                {
                    return(_details.ConvertLongValue(Convert.ToInt64(number)));
                }

                return(number);
            }

            if (value is String)
            {
                var text = (string)value;

                if (TrimStrings)
                {
                    text = text.Trim();
                }

                if (ConvertEmptyStringToNull && text.Length < 1)
                {
                    return(null);
                }

                if (RecognizeIntegers && text.Length > 0 && text.Length < 21 && !Char.IsLetter(text, 0))
                {
                    long number;
                    if (Int64.TryParse(text, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out number))
                    {
                        if (number.ToString(CultureInfo.InvariantCulture) == text)
                        {
                            return(_details.ConvertLongValue(number));
                        }
                    }
                }

                return(text);
            }

            return(value);
        }
Exemplo n.º 2
0
        private object ConvertValue(object value)
        {
            switch (value)
            {
            case null:
                return(null);

            case ulong value1:
            {
                var number = value1;
                value = number <= long.MaxValue ? (long)number : (decimal)number;
                break;
            }
            }

            switch (value)
            {
            case bool _ when _details.SupportsBoolean:
                return(value);

            case bool b:
                value = b
                        ? 1
                        : 0;
                break;

            case decimal _ when _details.SupportsDecimal:
                return(value);

            case decimal _:
                value = Convert.ToString(value, CultureInfo.InvariantCulture);
                break;
            }

            switch (value)
            {
            case int _:
            case long _:
            case byte _:
            case sbyte _:
            case short _:
            case ushort _:
            case uint _:
            case Enum _:
                return(_details.ConvertLongValue(Convert.ToInt64(value)));

            case double _:
            case float _:
            {
                var number = Convert.ToDouble(value);
                if (RecognizeIntegers && number.IsSafeInteger())
                {
                    return(_details.ConvertLongValue(Convert.ToInt64(number)));
                }

                return(number);
            }

            case string s:
            {
                var text = s;

                if (TrimStrings)
                {
                    text = text.Trim();
                }

                if (ConvertEmptyStringToNull && text.Length < 1)
                {
                    return(null);
                }

                if (!RecognizeIntegers || text.Length <= 0 || text.Length >= 21 || char.IsLetter(text, 0))
                {
                    return(text);
                }

                if (!long.TryParse(text, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture,
                                   out var number))
                {
                    return(text);
                }

                return(number.ToString(CultureInfo.InvariantCulture) == text
                            ? _details.ConvertLongValue(number)
                            : text);
            }

            default:
                return(value);
            }
        }