当前位置: 首页>>代码示例>>C#>>正文


C# JsonTextReader.ReadAsInt32方法代码示例

本文整理汇总了C#中Raven.Imports.Newtonsoft.Json.JsonTextReader.ReadAsInt32方法的典型用法代码示例。如果您正苦于以下问题:C# JsonTextReader.ReadAsInt32方法的具体用法?C# JsonTextReader.ReadAsInt32怎么用?C# JsonTextReader.ReadAsInt32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Raven.Imports.Newtonsoft.Json.JsonTextReader的用法示例。


在下文中一共展示了JsonTextReader.ReadAsInt32方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ReadInt32Overflow_Negative

    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();
          });
      }
    }
开发者ID:WimVergouwe,项目名称:ravendb,代码行数:21,代码来源:JsonTextReaderTest.cs

示例2: ReadOctalNumberAsInt32

    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());
    }
开发者ID:WimVergouwe,项目名称:ravendb,代码行数:28,代码来源:JsonTextReaderTest.cs

示例3: ReadAsIntDecimal

    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();
        });
    }
开发者ID:WimVergouwe,项目名称:ravendb,代码行数:19,代码来源:JsonTextReaderTest.cs

示例4: ReadInt32Overflow_Negative

        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.");
            }
        }
开发者ID:j2jensen,项目名称:ravendb,代码行数:19,代码来源:JsonTextReaderTest.cs

示例5: ParseIntegers

    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());
    }
开发者ID:WimVergouwe,项目名称:ravendb,代码行数:37,代码来源:JsonTextReaderTest.cs

示例6: ReadInvalidNonBase10Number

        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.");
        }
开发者ID:j2jensen,项目名称:ravendb,代码行数:16,代码来源:JsonTextReaderTest.cs

示例7: ReadAsIntDecimal

        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.");
        }
开发者ID:j2jensen,项目名称:ravendb,代码行数:14,代码来源:JsonTextReaderTest.cs

示例8: ReadIntegerWithErrorInArray

        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());
        }
开发者ID:j2jensen,项目名称:ravendb,代码行数:27,代码来源:JsonTextReaderTest.cs

示例9: ParseIntegers

        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.");
        }
开发者ID:j2jensen,项目名称:ravendb,代码行数:40,代码来源:JsonTextReaderTest.cs

示例10: ReadIntegerWithError

        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());
        }
开发者ID:j2jensen,项目名称:ravendb,代码行数:21,代码来源:JsonTextReaderTest.cs


注:本文中的Raven.Imports.Newtonsoft.Json.JsonTextReader.ReadAsInt32方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。