コード例 #1
0
        public void ReadInvalidNonBase10Number()
        {
            string json = "0aq2dun13.hod";

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

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

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

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

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

            AssertException.Throws<JsonReaderException>(() => { reader.ReadAsInt32(); }, "Unexpected character encountered while parsing number: q. Path '', line 1, position 2.");
        }
コード例 #2
0
        public void ReadInt32Overflow_Negative()
        {
            long i = int.MinValue;

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

            for (int j = 1; j < 1000; j++)
            {
                long total = -j + i;
                AssertException.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.");
            }
        }
コード例 #3
0
        public void ReadAsIntDecimal()
        {
            string json = @"{""Name"": 1.1}";

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

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.StartObject, reader.TokenType);

            Assert.True(reader.Read());
            Assert.Equal(JsonToken.PropertyName, reader.TokenType);

            AssertException.Throws<JsonReaderException>(() => { reader.ReadAsInt32(); }, "Input string '1.1' is not a valid integer. Path 'Name', line 1, position 12.");
        }
コード例 #4
0
        public void ReadOctalNumberAsInt32()
        {
            StringReader s = new StringReader(@"[0372, 0xFA, 0XFA]");
            JsonTextReader jsonReader = new JsonTextReader(s);

            Assert.True(jsonReader.Read());
            Assert.Equal(JsonToken.StartArray, jsonReader.TokenType);

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

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

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

            Assert.True(jsonReader.Read());
            Assert.Equal(JsonToken.EndArray, jsonReader.TokenType);

            Assert.False(jsonReader.Read());
        }
コード例 #5
0
        public void ReadIntegerWithErrorInArray()
        {
            string json = @"[
  333333333333333333333333333333333333333,
  3.3,
  ,
  0f
]";

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

            Assert.True(jsonTextReader.Read());
            Assert.Equal(JsonToken.StartArray, jsonTextReader.TokenType);

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

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

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

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

            Assert.True(jsonTextReader.Read());
            Assert.Equal(JsonToken.EndArray, jsonTextReader.TokenType);

            Assert.False(jsonTextReader.Read());
        }
コード例 #6
0
        public void ParseIntegers()
        {
            JsonTextReader reader = null;

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

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

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

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

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

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

            reader = new JsonTextReader(new StringReader(long.MaxValue.ToString()));
            AssertException.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"));
            AssertException.Throws<JsonReaderException>(() => reader.ReadAsInt32(), "Unexpected character encountered while parsing number: s. Path '', line 1, position 77.");

            reader = new JsonTextReader(new StringReader("1E-06"));
            AssertException.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"));
            AssertException.Throws<JsonReaderException>(() => reader.ReadAsInt32(), "Input string '1.1' is not a valid integer. Path '', line 1, position 3.");

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

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

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

            Assert.True(jsonTextReader.Read());
            Assert.Equal(JsonToken.StartObject, jsonTextReader.TokenType);

            Assert.True(jsonTextReader.Read());
            Assert.Equal(JsonToken.PropertyName, jsonTextReader.TokenType);

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

            Assert.True(jsonTextReader.Read());
            Assert.Equal(JsonToken.EndObject, jsonTextReader.TokenType);

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