示例#1
0
        private object ParseNumber()
        {
            long num2;

            this.ConsumeToken();
            int  startIndex = this.index - 1;
            bool flag       = false;

            do
            {
                if (this.index == this.json.Length)
                {
                    break;
                }
                char ch = this.json[this.index];
                if (((ch < '0') || (ch > '9')) && (((ch != '.') && (ch != '-')) && (((ch != '+') && (ch != 'e')) && (ch != 'E'))))
                {
                    break;
                }
                if (((ch == '.') || (ch == 'e')) || (ch == 'E'))
                {
                    flag = true;
                }
            }while (++this.index != this.json.Length);
            if (flag)
            {
                return(double.Parse(this.json.Substring(startIndex, this.index - startIndex), NumberFormatInfo.InvariantInfo));
            }
            return(JSON.CreateLong(out num2, this.json, startIndex, this.index - startIndex));
        }
示例#2
0
        private object ParseNumber()
        {
            ConsumeToken();

            // Need to start back one place because the first digit is also a token and would have been consumed
            var  startIndex = index - 1;
            bool dec        = false;

            do
            {
                if (index == json.Length)
                {
                    break;
                }
                var c = json[index];

                if ((c >= '0' && c <= '9') || c == '.' || c == '-' || c == '+' || c == 'e' || c == 'E')
                {
                    if (c == '.' || c == 'e' || c == 'E')
                    {
                        dec = true;
                    }
                    if (++index == json.Length)
                    {
                        break;//throw new Exception("Unexpected end of string whilst parsing number");
                    }
                    continue;
                }
                break;
            } while (true);

            if (dec)
            {
                string s = json.Substring(startIndex, index - startIndex);
                return(double.Parse(s, NumberFormatInfo.InvariantInfo));
            }
            if (index - startIndex < 20 && json[startIndex] != '-')
            {
                return(JSON.CreateLong(json, startIndex, index - startIndex));
            }
            else
            {
                string s = json.Substring(startIndex, index - startIndex);
                //return s;
                return(decimal.Parse(s, NumberFormatInfo.InvariantInfo));
            }
        }
示例#3
0
        private object ChangeType(object value, Type conversionType)
        {
            if (conversionType == typeof(int))
            {
                string s = value as string;
                if (s == null)
                {
                    return((int)((long)value));
                }
                else
                {
                    return(CreateInteger(s, 0, s.Length));
                }
            }
            else if (conversionType == typeof(long))
            {
                string s = value as string;
                if (s == null)
                {
                    return((long)value);
                }
                else
                {
                    return(JSON.CreateLong(s, 0, s.Length));
                }
            }
            else if (conversionType == typeof(string))
            {
                return((string)value);
            }

            else if (conversionType.IsEnum)
            {
                return(CreateEnum(conversionType, value));
            }

            else if (conversionType == typeof(DateTime))
            {
                return(CreateDateTime((string)value));
            }

            else if (conversionType == typeof(DateTimeOffset))
            {
                return(CreateDateTimeOffset((string)value));
            }

            else if (Reflection.Instance.IsTypeRegistered(conversionType))
            {
                return(Reflection.Instance.CreateCustom((string)value, conversionType));
            }

            // 8-30-2014 - James Brooks - Added code for nullable types.
            if (IsNullable(conversionType))
            {
                if (value == null)
                {
                    return(value);
                }
                conversionType = UnderlyingTypeOf(conversionType);
            }

            // 8-30-2014 - James Brooks - Nullable Guid is a special case so it was moved after the "IsNullable" check.
            if (conversionType == typeof(Guid))
            {
                return(CreateGuid((string)value));
            }

            // 2016-04-02 - Enrico Padovani - proper conversion of byte[] back from string
            if (conversionType == typeof(byte[]))
            {
                return(Convert.FromBase64String((string)value));
            }

            if (conversionType == typeof(TimeSpan))
            {
                return(new TimeSpan((long)value));
            }

            return(Convert.ChangeType(value, conversionType, CultureInfo.InvariantCulture));
        }