Ejemplo n.º 1
0
        private void ParseNumber()
        {
            ShiftBufferIfNeeded();

            char firstChar = _chars[_charPos];
            int initialPosition = _charPos;

            ReadNumberIntoBuffer();

            // set state to PostValue now so that if there is an error parsing the number then the reader can continue
            SetPostValueState(true);

            _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition);

            object numberValue;
            JsonToken numberType;

            bool singleDigit = (char.IsDigit(firstChar) && _stringReference.Length == 1);
            bool nonBase10 = (firstChar == '0' && _stringReference.Length > 1
                              && _stringReference.Chars[_stringReference.StartIndex + 1] != '.'
                              && _stringReference.Chars[_stringReference.StartIndex + 1] != 'e'
                              && _stringReference.Chars[_stringReference.StartIndex + 1] != 'E');

	        if (_readType == ReadType.ReadAsInt32)
	        {
		        if (singleDigit)
		        {
			        // digit char values start at 48
			        numberValue = firstChar - 48;
		        }
		        else if (nonBase10)
		        {
			        string number = _stringReference.ToString();

			        try
			        {
				        int integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
					        ? Convert.ToInt32(number, 16)
					        : Convert.ToInt32(number, 8);

				        numberValue = integer;
			        }
			        catch (Exception ex)
			        {
				        throw JsonReaderException.Create(this, "Input string '{0}' is not a valid integer.".FormatWith(CultureInfo.InvariantCulture, number), ex);
			        }
		        }
		        else
		        {
			        int value;
			        ParseResult parseResult = ConvertUtils.Int32TryParse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length, out value);
			        if (parseResult == ParseResult.Success)
				        numberValue = value;
			        else if (parseResult == ParseResult.Overflow)
				        throw JsonReaderException.Create(this, "JSON integer {0} is too large or small for an Int32.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
			        else
				        throw JsonReaderException.Create(this, "Input string '{0}' is not a valid integer.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
		        }

		        numberType = JsonToken.Integer;
	        }
	        else
	        {
				const NumberStyles numberStyles = NumberStyles.Number | NumberStyles.AllowExponent; 
				if (_readType == ReadType.ReadAsDecimal)
		        {
			        if (singleDigit)
			        {
				        // digit char values start at 48
				        numberValue = (decimal) firstChar - 48;
			        }
			        else if (nonBase10)
			        {
				        string number = _stringReference.ToString();

				        try
				        {
					        // decimal.Parse doesn't support parsing hexadecimal values
					        long integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
						        ? Convert.ToInt64(number, 16)
						        : Convert.ToInt64(number, 8);

					        numberValue = Convert.ToDecimal(integer);
				        }
				        catch (Exception ex)
				        {
					        throw JsonReaderException.Create(this, "Input string '{0}' is not a valid decimal.".FormatWith(CultureInfo.InvariantCulture, number), ex);
				        }
			        }
			        else
			        {
				        string number = _stringReference.ToString();

				        decimal value;
						if (decimal.TryParse(number, numberStyles, CultureInfo.InvariantCulture, out value))
					        numberValue = value;
				        else
					        throw JsonReaderException.Create(this, "Input string '{0}' is not a valid decimal.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
			        }

			        numberType = JsonToken.Float;
		        }
		        else
		        {
			        if (singleDigit)
			        {
				        // digit char values start at 48
				        numberValue = (long) firstChar - 48;
				        numberType = JsonToken.Integer;
			        }
			        else if (nonBase10)
			        {
				        string number = _stringReference.ToString();

				        try
				        {
					        numberValue = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
						        ? Convert.ToInt64(number, 16)
						        : Convert.ToInt64(number, 8);
				        }
				        catch (Exception ex)
				        {
					        throw JsonReaderException.Create(this, "Input string '{0}' is not a valid number.".FormatWith(CultureInfo.InvariantCulture, number), ex);
				        }

				        numberType = JsonToken.Integer;
			        }
			        else
			        {
				        long value;
				        ParseResult parseResult = ConvertUtils.Int64TryParse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length, out value);
				        if (parseResult == ParseResult.Success)
				        {
					        numberValue = value;
					        numberType = JsonToken.Integer;
				        }
				        else if (parseResult == ParseResult.Overflow)
				        {
#if !(NET20 || NET35 || PORTABLE40 || PORTABLE)
					        string number = _stringReference.ToString();

					        if (number.Length > MaximumJavascriptIntegerCharacterLength)
						        throw JsonReaderException.Create(this, "JSON integer {0} is too large to parse.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));

					        numberValue = BigIntegerParse(number, CultureInfo.InvariantCulture);
					        numberType = JsonToken.Integer;
#else
                        throw JsonReaderException.Create(this, "JSON integer {0} is too large or small for an Int64.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
#endif
				        }
				        else
				        {
					        string number = _stringReference.ToString();
							switch (_floatParseHandling)
							{
								case FloatParseHandling.Decimal:
									decimal d;
									if (decimal.TryParse(number, numberStyles, CultureInfo.InvariantCulture, out d))
										numberValue = d;
									else
										throw JsonReaderException.Create(this, "Input string '{0}' is not a valid decimal.".FormatWith(CultureInfo.InvariantCulture, number));
									break;
								case FloatParseHandling.PreferDecimalFallbackToDouble:
									decimal result;
									if (decimal.TryParse(number, numberStyles, CultureInfo.InvariantCulture, out result) == false)
									{
										goto default;
									}
									numberValue = result;
									break;
								default:
									double d1;
									if (double.TryParse(number, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out d1))
										numberValue = d1;
									else
										throw JsonReaderException.Create(this, "Input string '{0}' is not a valid number.".FormatWith(CultureInfo.InvariantCulture, number));
									break;
							}

					        numberType = JsonToken.Float;
				        }
			        }
		        }
	        }

	        ClearRecentString();

            // index has already been updated
            SetToken(numberType, numberValue, false);
        }
Ejemplo n.º 2
0
		private async Task ParseNumber()
		{
			ShiftBufferIfNeeded();

			char firstChar = _chars[_charPos];
			int initialPosition = _charPos;

			await ReadNumberIntoBuffer().ConfigureAwait(false);

			_stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition);

			object numberValue;
			JsonToken numberType;

			bool singleDigit = (char.IsDigit(firstChar) && _stringReference.Length == 1);
			bool nonBase10 = (firstChar == '0' && _stringReference.Length > 1
			  && _stringReference.Chars[_stringReference.StartIndex + 1] != '.'
			  && _stringReference.Chars[_stringReference.StartIndex + 1] != 'e'
			  && _stringReference.Chars[_stringReference.StartIndex + 1] != 'E');

			if (_readType == ReadType.ReadAsInt32)
			{
				if (singleDigit)
				{
					// digit char values start at 48
					numberValue = firstChar - 48;
				}
				else if (nonBase10)
				{
					string number = _stringReference.ToString();

					// decimal.Parse doesn't support parsing hexadecimal values
					int integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
									 ? Convert.ToInt32(number, 16)
									 : Convert.ToInt32(number, 8);

					numberValue = integer;
				}
				else
				{
					string number = _stringReference.ToString();

					numberValue = Convert.ToInt32(number, CultureInfo.InvariantCulture);
				}

				numberType = JsonToken.Integer;
			}
			else if (_readType == ReadType.ReadAsDecimal)
			{
				if (singleDigit)
				{
					// digit char values start at 48
					numberValue = (decimal)firstChar - 48;
				}
				else if (nonBase10)
				{
					string number = _stringReference.ToString();

					// decimal.Parse doesn't support parsing hexadecimal values
					long integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
									 ? Convert.ToInt64(number, 16)
									 : Convert.ToInt64(number, 8);

					numberValue = Convert.ToDecimal(integer);
				}
				else
				{
					string number = _stringReference.ToString();

					numberValue = decimal.Parse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture);
				}

				numberType = JsonToken.Float;
			}
			else
			{
				if (singleDigit)
				{
					// digit char values start at 48
					numberValue = (long)firstChar - 48;
					numberType = JsonToken.Integer;
				}
				else if (nonBase10)
				{
					string number = _stringReference.ToString();

					numberValue = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
									? Convert.ToInt64(number, 16)
									: Convert.ToInt64(number, 8);
					numberType = JsonToken.Integer;
				}
				else
				{
					string number = _stringReference.ToString();

					// it's faster to do 3 indexof with single characters than an indexofany
					if (number.IndexOf('E') != -1 || number.IndexOf('e') != -1)
					{
						numberValue = Convert.ToDouble(number, CultureInfo.InvariantCulture);
						numberType = JsonToken.Float;
					}
					else if (number.IndexOf('.') != -1)
					{
						numberValue = Convert.ToDecimal(number, CultureInfo.InvariantCulture);
						numberType = JsonToken.Float;
					}
					else
					{
						try
						{
							numberValue = Convert.ToInt64(number, CultureInfo.InvariantCulture);
						}
						catch (OverflowException ex)
						{
							throw JsonReaderException.Create(this, Path, "JSON integer {0} is too large or small for an Int64.".FormatWith(CultureInfo.InvariantCulture, number), ex);
						}

						numberType = JsonToken.Integer;
					}
				}
			}

			ClearRecentString();

			SetToken(numberType, numberValue);
		}
Ejemplo n.º 3
0
        private void ParseConstructor()
        {
            if (MatchValueWithTrailingSeparator("new"))
            {
                EatWhitespace(false);

                int initialPosition = _charPos;
                int endPosition;

                while (true)
                {
                    char currentChar = _chars[_charPos];
                    if (currentChar == '\0')
                    {
                        if (_charsUsed == _charPos)
                        {
                            if (ReadData(true) == 0)
                                throw JsonReaderException.Create(this, "Unexpected end while parsing constructor.");
                        }
                        else
                        {
                            endPosition = _charPos;
                            _charPos++;
                            break;
                        }
                    }
                    else if (char.IsLetterOrDigit(currentChar))
                    {
                        _charPos++;
                    }
                    else if (currentChar == StringUtils.CarriageReturn)
                    {
                        endPosition = _charPos;
                        ProcessCarriageReturn(true);
                        break;
                    }
                    else if (currentChar == StringUtils.LineFeed)
                    {
                        endPosition = _charPos;
                        ProcessLineFeed();
                        break;
                    }
                    else if (char.IsWhiteSpace(currentChar))
                    {
                        endPosition = _charPos;
                        _charPos++;
                        break;
                    }
                    else if (currentChar == '(')
                    {
                        endPosition = _charPos;
                        break;
                    }
                    else
                    {
                        throw JsonReaderException.Create(this, "Unexpected character while parsing constructor: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar));
                    }
                }

                _stringReference = new StringReference(_chars, initialPosition, endPosition - initialPosition);
                string constructorName = _stringReference.ToString();

                EatWhitespace(false);

                if (_chars[_charPos] != '(')
                    throw JsonReaderException.Create(this, "Unexpected character while parsing constructor: {0}.".FormatWith(CultureInfo.InvariantCulture, _chars[_charPos]));

                _charPos++;

                ClearRecentString();

                SetToken(JsonToken.StartConstructor, constructorName);
            }
            else
            {
                throw JsonReaderException.Create(this, "Unexpected content while parsing JSON.");
            }
        }
Ejemplo n.º 4
0
    private void ParseNumber()
    {
      ShiftBufferIfNeeded();

      char firstChar = _chars[_charPos];
      int initialPosition = _charPos;

      ReadNumberIntoBuffer();

      _stringReference = new StringReference(_chars, initialPosition, _charPos - initialPosition);

      object numberValue;
      JsonToken numberType;

      bool singleDigit = (char.IsDigit(firstChar) && _stringReference.Length == 1);
      bool nonBase10 = (firstChar == '0' && _stringReference.Length > 1
        && _stringReference.Chars[_stringReference.StartIndex + 1] != '.'
        && _stringReference.Chars[_stringReference.StartIndex + 1] != 'e'
        && _stringReference.Chars[_stringReference.StartIndex + 1] != 'E');

      if (_readType == ReadType.ReadAsInt32)
      {
        if (singleDigit)
        {
          // digit char values start at 48
          numberValue = firstChar - 48;
        }
        else if (nonBase10)
        {
          string number = _stringReference.ToString();

          // decimal.Parse doesn't support parsing hexadecimal values
          int integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
                           ? Convert.ToInt32(number, 16)
                           : Convert.ToInt32(number, 8);

          numberValue = integer;
        }
        else
        {
          numberValue = ConvertUtils.Int32Parse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length);
        }

        numberType = JsonToken.Integer;
      }
      else if (_readType == ReadType.ReadAsDecimal)
      {
        if (singleDigit)
        {
          // digit char values start at 48
          numberValue = (decimal)firstChar - 48;
        }
        else if (nonBase10)
        {
          string number = _stringReference.ToString();

          // decimal.Parse doesn't support parsing hexadecimal values
          long integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
                           ? Convert.ToInt64(number, 16)
                           : Convert.ToInt64(number, 8);

          numberValue = Convert.ToDecimal(integer);
        }
        else
        {
          string number = _stringReference.ToString();

          numberValue = decimal.Parse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture);
        }

        numberType = JsonToken.Float;
      }
      else
      {
        if (singleDigit)
        {
          // digit char values start at 48
          numberValue = (long)firstChar - 48;
          numberType = JsonToken.Integer;
        }
        else if (nonBase10)
        {
          string number = _stringReference.ToString();

          numberValue = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
                          ? Convert.ToInt64(number, 16)
                          : Convert.ToInt64(number, 8);
          numberType = JsonToken.Integer;
        }
        else
        {
          long value;
          ParseResult parseResult = ConvertUtils.Int64TryParse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length, out value);
          if (parseResult == ParseResult.Success)
          {
            numberValue = value;
            numberType = JsonToken.Integer;
          }
          else if (parseResult == ParseResult.Invalid)
          {
            string number = _stringReference.ToString();

            if (_floatParseHandling == FloatParseHandling.Decimal)
              numberValue = decimal.Parse(number, NumberStyles.Number | NumberStyles.AllowExponent, CultureInfo.InvariantCulture);
            else
              numberValue = Convert.ToDouble(number, CultureInfo.InvariantCulture);

            numberType = JsonToken.Float;
          }
          else if (parseResult == ParseResult.Overflow)
          {
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE40 || PORTABLE)
            string number = _stringReference.ToString();
            numberValue = BigInteger.Parse(number, CultureInfo.InvariantCulture);
            numberType = JsonToken.Integer;
#else
            // todo - validate number was a valid integer to make sure overflow was the reason for failure
            throw JsonReaderException.Create(this, "JSON integer {0} is too large or small for an Int64.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
#endif
          }
          else
          {
            throw JsonReaderException.Create(this, "Unknown error parsing integer.");
          }
        }
      }

      ClearRecentString();

      SetToken(numberType, numberValue);
    }