public void ReadInt32Overflow_Negative()
    {
      long i = int.MinValue;

      JsonTextReader reader = new JsonTextReader(new StringReader(i.ToString(CultureInfo.InvariantCulture)));
      reader.Read();
      Assert.AreEqual(typeof(long), reader.ValueType);
      Assert.AreEqual(i, reader.Value);

      for (int j = 1; j < 1000; j++)
      {
        long total = -j + i;
        ExceptionAssert.Throws<OverflowException>(
          "Arithmetic operation resulted in an overflow.",
          () =>
          {
            reader = new JsonTextReader(new StringReader(total.ToString(CultureInfo.InvariantCulture)));
            reader.ReadAsInt32();
          });
      }
    }
    public void ReadOctalNumberAsInt32()
    {
      StringReader s = new StringReader(@"[0372, 0xFA, 0XFA]");
      JsonTextReader jsonReader = new JsonTextReader(s);

      Assert.IsTrue(jsonReader.Read());
      Assert.AreEqual(JsonToken.StartArray, jsonReader.TokenType);

      jsonReader.ReadAsInt32();
      Assert.AreEqual(JsonToken.Integer, jsonReader.TokenType);
      Assert.AreEqual(typeof(int), jsonReader.ValueType);
      Assert.AreEqual(250, jsonReader.Value);

      jsonReader.ReadAsInt32();
      Assert.AreEqual(JsonToken.Integer, jsonReader.TokenType);
      Assert.AreEqual(typeof(int), jsonReader.ValueType);
      Assert.AreEqual(250, jsonReader.Value);

      jsonReader.ReadAsInt32();
      Assert.AreEqual(JsonToken.Integer, jsonReader.TokenType);
      Assert.AreEqual(typeof(int), jsonReader.ValueType);
      Assert.AreEqual(250, jsonReader.Value);

      Assert.IsTrue(jsonReader.Read());
      Assert.AreEqual(JsonToken.EndArray, jsonReader.TokenType);

      Assert.IsFalse(jsonReader.Read());
    }
    public void ReadAsIntDecimal()
    {
      string json = @"{""Name"": 1.1}";

      JsonTextReader reader = new JsonTextReader(new StringReader(json));

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.StartObject, reader.TokenType);

      Assert.IsTrue(reader.Read());
      Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);

      ExceptionAssert.Throws<FormatException>(
        "Input string was not in a correct format.",
        () =>
        {
          reader.ReadAsInt32();
        });
    }
        public void ReadInt32Overflow_Negative()
        {
            long i = int.MinValue;

            JsonTextReader reader = new JsonTextReader(new StringReader(i.ToString(CultureInfo.InvariantCulture)));
            reader.Read();
            Assert.AreEqual(typeof(long), reader.ValueType);
            Assert.AreEqual(i, reader.Value);

            for (int j = 1; j < 1000; j++)
            {
                long total = -j + i;
                ExceptionAssert.Throws<JsonReaderException>(() =>
                {
                    reader = new JsonTextReader(new StringReader(total.ToString(CultureInfo.InvariantCulture)));
                    reader.ReadAsInt32();
                }, "JSON integer " + total + " is too large or small for an Int32. Path '', line 1, position 11.");
            }
        }
    public void ParseIntegers()
    {
      JsonTextReader reader = null;

      reader = new JsonTextReader(new StringReader("1"));
      Assert.AreEqual(1, reader.ReadAsInt32());

      reader = new JsonTextReader(new StringReader("-1"));
      Assert.AreEqual(-1, reader.ReadAsInt32());

      reader = new JsonTextReader(new StringReader("0"));
      Assert.AreEqual(0, reader.ReadAsInt32());

      reader = new JsonTextReader(new StringReader("-0"));
      Assert.AreEqual(0, reader.ReadAsInt32());

      reader = new JsonTextReader(new StringReader(int.MaxValue.ToString()));
      Assert.AreEqual(int.MaxValue, reader.ReadAsInt32());

      reader = new JsonTextReader(new StringReader(int.MinValue.ToString()));
      Assert.AreEqual(int.MinValue, reader.ReadAsInt32());

      reader = new JsonTextReader(new StringReader(long.MaxValue.ToString()));
      ExceptionAssert.Throws<OverflowException>("Arithmetic operation resulted in an overflow.", () => reader.ReadAsInt32());

      reader = new JsonTextReader(new StringReader("1E-06"));
      ExceptionAssert.Throws<FormatException>("Input string was not in a correct format.", () => reader.ReadAsInt32());

      reader = new JsonTextReader(new StringReader("1.1"));
      ExceptionAssert.Throws<FormatException>("Input string was not in a correct format.", () => reader.ReadAsInt32());

      reader = new JsonTextReader(new StringReader(""));
      Assert.AreEqual(null, reader.ReadAsInt32());

      reader = new JsonTextReader(new StringReader("-"));
      ExceptionAssert.Throws<FormatException>("Input string was not in a correct format.", () => reader.ReadAsInt32());
    }
        public void ReadInvalidNonBase10Number()
        {
            string json = "0aq2dun13.hod";

            JsonTextReader reader = new JsonTextReader(new StringReader(json));

            ExceptionAssert.Throws<JsonReaderException>(() => { reader.Read(); }, "Unexpected character encountered while parsing number: q. Path '', line 1, position 2.");

            reader = new JsonTextReader(new StringReader(json));

            ExceptionAssert.Throws<JsonReaderException>(() => { reader.ReadAsDecimal(); }, "Unexpected character encountered while parsing number: q. Path '', line 1, position 2.");

            reader = new JsonTextReader(new StringReader(json));

            ExceptionAssert.Throws<JsonReaderException>(() => { reader.ReadAsInt32(); }, "Unexpected character encountered while parsing number: q. Path '', line 1, position 2.");
        }
        public void ReadAsIntDecimal()
        {
            string json = @"{""Name"": 1.1}";

            JsonTextReader reader = new JsonTextReader(new StringReader(json));

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.StartObject, reader.TokenType);

            Assert.IsTrue(reader.Read());
            Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);

            ExceptionAssert.Throws<JsonReaderException>(() => { reader.ReadAsInt32(); }, "Input string '1.1' is not a valid integer. Path 'Name', line 1, position 12.");
        }
        public void ReadIntegerWithErrorInArray()
        {
            string json = @"[
  333333333333333333333333333333333333333,
  3.3,
  ,
  0f
]";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));

            Assert.IsTrue(jsonTextReader.Read());
            Assert.AreEqual(JsonToken.StartArray, jsonTextReader.TokenType);

            ExceptionAssert.Throws<JsonReaderException>(() => jsonTextReader.ReadAsInt32(), "JSON integer 333333333333333333333333333333333333333 is too large or small for an Int32. Path '[0]', line 2, position 42.");

            ExceptionAssert.Throws<JsonReaderException>(() => jsonTextReader.ReadAsInt32(), "Input string '3.3' is not a valid integer. Path '[1]', line 3, position 6.");

            ExceptionAssert.Throws<JsonReaderException>(() => jsonTextReader.ReadAsInt32(), "Error reading integer. Unexpected token: Undefined. Path '[2]', line 4, position 3.");

            ExceptionAssert.Throws<JsonReaderException>(() => jsonTextReader.ReadAsInt32(), "Input string '0f' is not a valid integer. Path '[3]', line 5, position 5.");

            Assert.IsTrue(jsonTextReader.Read());
            Assert.AreEqual(JsonToken.EndArray, jsonTextReader.TokenType);

            Assert.IsFalse(jsonTextReader.Read());
        }
        public void ParseIntegers()
        {
            JsonTextReader reader = null;

            reader = new JsonTextReader(new StringReader("1"));
            Assert.AreEqual(1, reader.ReadAsInt32());

            reader = new JsonTextReader(new StringReader("-1"));
            Assert.AreEqual(-1, reader.ReadAsInt32());

            reader = new JsonTextReader(new StringReader("0"));
            Assert.AreEqual(0, reader.ReadAsInt32());

            reader = new JsonTextReader(new StringReader("-0"));
            Assert.AreEqual(0, reader.ReadAsInt32());

            reader = new JsonTextReader(new StringReader(int.MaxValue.ToString()));
            Assert.AreEqual(int.MaxValue, reader.ReadAsInt32());

            reader = new JsonTextReader(new StringReader(int.MinValue.ToString()));
            Assert.AreEqual(int.MinValue, reader.ReadAsInt32());

            reader = new JsonTextReader(new StringReader(long.MaxValue.ToString()));
            ExceptionAssert.Throws<JsonReaderException>(() => reader.ReadAsInt32(), "JSON integer 9223372036854775807 is too large or small for an Int32. Path '', line 1, position 19.");

            reader = new JsonTextReader(new StringReader("9999999999999999999999999999999999999999999999999999999999999999999999999999asdasdasd"));
            ExceptionAssert.Throws<JsonReaderException>(() => reader.ReadAsInt32(), "Unexpected character encountered while parsing number: s. Path '', line 1, position 77.");

            reader = new JsonTextReader(new StringReader("1E-06"));
            ExceptionAssert.Throws<JsonReaderException>(() => reader.ReadAsInt32(), "Input string '1E-06' is not a valid integer. Path '', line 1, position 5.");

            reader = new JsonTextReader(new StringReader("1.1"));
            ExceptionAssert.Throws<JsonReaderException>(() => reader.ReadAsInt32(), "Input string '1.1' is not a valid integer. Path '', line 1, position 3.");

            reader = new JsonTextReader(new StringReader(""));
            Assert.AreEqual(null, reader.ReadAsInt32());

            reader = new JsonTextReader(new StringReader("-"));
            ExceptionAssert.Throws<JsonReaderException>(() => reader.ReadAsInt32(), "Input string '-' is not a valid integer. Path '', line 1, position 1.");
        }
        public void ReadIntegerWithError()
        {
            string json = @"{
    ChildId: 333333333333333333333333333333333333333
}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));

            Assert.IsTrue(jsonTextReader.Read());
            Assert.AreEqual(JsonToken.StartObject, jsonTextReader.TokenType);

            Assert.IsTrue(jsonTextReader.Read());
            Assert.AreEqual(JsonToken.PropertyName, jsonTextReader.TokenType);

            ExceptionAssert.Throws<JsonReaderException>(() => jsonTextReader.ReadAsInt32(), "JSON integer 333333333333333333333333333333333333333 is too large or small for an Int32. Path 'ChildId', line 2, position 53.");

            Assert.IsTrue(jsonTextReader.Read());
            Assert.AreEqual(JsonToken.EndObject, jsonTextReader.TokenType);

            Assert.IsFalse(jsonTextReader.Read());
        }