Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Src/Newtonsoft.Json.Tests/Issues/Issue2664.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

#if !(NET20 || NET35 || PORTABLE || PORTABLE40)
#if DNXCORE50
using Xunit;
using Test = Xunit.FactAttribute;
using Assert = Newtonsoft.Json.Tests.XUnitAssert;
#else
using NUnit.Framework;
#endif
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;

namespace Newtonsoft.Json.Tests.Issues
{
[TestFixture]
public class Issue2664 : TestFixtureBase
{
[Test]
public void Test()
{
ExceptionAssert.Throws<JsonReaderException>(() => JsonConvert.DeserializeObject<SomData>(@"{""aInt"":1, ""ALong"":2.2}"), "Input string '2.2' is not a valid integer. Path 'ALong', line 1, position 22.");

ExceptionAssert.Throws<JsonReaderException>(() => JsonConvert.DeserializeObject<SomData>(@"{""aInt"":1.2, ""ALong"":2}"), "Input string '1.2' is not a valid integer. Path 'aInt', line 1, position 11.");
}

private class SomData
{
public int AInt { get; set; }
public long ALong { get; set; }
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ await ExceptionAssert.ThrowsAsync<JsonReaderException>(
"After parsing a value an unexpected character was encountered: 1. Path '[0]', line 1, position 3.");
}

[Test]
public async Task ReadAsInt64_MissingCommaAsync()
{
string json = "[0 1 2]";
JsonTextReader reader = new JsonTextReader(new StringReader(json));

Assert.IsTrue(await reader.ReadAsync());
Assert.AreEqual(0, (int)await reader.ReadAsInt64Async());

await ExceptionAssert.ThrowsAsync<JsonReaderException>(
() => reader.ReadAsInt64Async(),
"After parsing a value an unexpected character was encountered: 1. Path '[0]', line 1, position 3.");
}

[Test]
public async Task ReadAsBoolean_MissingCommaAsync()
{
Expand Down Expand Up @@ -205,6 +219,8 @@ public async Task ReadInvalidNonBase10NumberAsync()
reader = new JsonTextReader(new StringReader(json));

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

await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () => { await reader.ReadAsInt64Async(); }, "Unexpected character encountered while parsing value: q. Path '', line 1, position 3.");
}

[Test]
Expand Down Expand Up @@ -494,6 +510,29 @@ public async Task ReadIntegerWithErrorAsync()
Assert.IsFalse(await jsonTextReader.ReadAsync());
}

[Test]
public async Task ReadInteger64WithErrorAsync()
{
string json = @"{
ChildId: 333333333333333333333333333333333333333
}";

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

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

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

await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () => await jsonTextReader.ReadAsInt64Async(), "JSON integer 333333333333333333333333333333333333333 is too large or small for an Int64. Path 'ChildId', line 2, position 52.");

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

Assert.IsFalse(await jsonTextReader.ReadAsync());
}

[Test]
public async Task ReadIntegerWithErrorInArrayAsync()
{
Expand Down Expand Up @@ -523,6 +562,35 @@ public async Task ReadIntegerWithErrorInArrayAsync()
Assert.IsFalse(await jsonTextReader.ReadAsync());
}

[Test]
public async Task ReadInteger64WithErrorInArrayAsync()
{
string json = @"[
333333333333333333333333333333333333333,
3.3,
,
0f
]";

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

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

await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () => await jsonTextReader.ReadAsInt64Async(), "JSON integer 333333333333333333333333333333333333333 is too large or small for an Int64. Path '[0]', line 2, position 41.");

await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () => await jsonTextReader.ReadAsInt64Async(), "Input string '3.3' is not a valid integer. Path '[1]', line 3, position 5.");

await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () => await jsonTextReader.ReadAsInt64Async(), "Unexpected character encountered while parsing value: ,. Path '[2]', line 4, position 3.");

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

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

Assert.IsFalse(await jsonTextReader.ReadAsync());
}

[Test]
public async Task ReadBytesWithErrorAsync()
{
Expand Down Expand Up @@ -865,6 +933,14 @@ public async Task ReadInt32WithBadCharacterAsync()
await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () => { await reader.ReadAsInt32Async(); }, "Unexpected character encountered while parsing value: t. Path '', line 1, position 1.");
}

[Test]
public async Task ReadInt64WithBadCharacterAsync()
{
JsonReader reader = new JsonTextReader(new StringReader(@"true"));

await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () => { await reader.ReadAsInt64Async(); }, "Unexpected character encountered while parsing value: t. Path '', line 1, position 1.");
}

[Test]
public async Task ReadNumberValue_CommaErrorsAsync()
{
Expand All @@ -880,6 +956,22 @@ await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () =>
Assert.IsTrue(await reader.ReadAsync());
}


[Test]
public async Task ReadNumberInt64Value_CommaErrorsAsync()
{
JsonTextReader reader = new JsonTextReader(new StringReader("[,1]"));
await reader.ReadAsync();

await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () =>
{
await reader.ReadAsInt64Async();
}, "Unexpected character encountered while parsing value: ,. Path '[0]', line 1, position 2.");

Assert.AreEqual(1, await reader.ReadAsInt64Async());
Assert.IsTrue(await reader.ReadAsync());
}

[Test]
public async Task ReadNumberValue_InvalidEndArrayAsync()
{
Expand All @@ -891,6 +983,17 @@ await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () =>
}, "Unexpected character encountered while parsing value: ]. Path '', line 1, position 1.");
}

[Test]
public async Task ReadNumberValue_Int64_InvalidEndArrayAsync()
{
JsonTextReader reader = new JsonTextReader(new StringReader("]"));

await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () =>
{
await reader.ReadAsInt64Async();
}, "Unexpected character encountered while parsing value: ]. Path '', line 1, position 1.");
}

[Test]
public async Task ReadNumberValue_CommaErrors_MultipleAsync()
{
Expand All @@ -907,6 +1010,22 @@ await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () =>
Assert.IsTrue(await reader.ReadAsync());
}

[Test]
public async Task ReadNumberInt64Value_CommaErrors_MultipleAsync()
{
JsonTextReader reader = new JsonTextReader(new StringReader("[1,,1]"));
await reader.ReadAsync();
await reader.ReadAsInt64Async();

await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () =>
{
await reader.ReadAsInt64Async();
}, "Unexpected character encountered while parsing value: ,. Path '[1]', line 1, position 4.");

Assert.AreEqual(1, await reader.ReadAsInt64Async());
Assert.IsTrue(await reader.ReadAsync());
}

[Test]
public async Task ReadAsString_UnexpectedEndAsync()
{
Expand Down Expand Up @@ -969,6 +1088,22 @@ await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () =>
Assert.IsTrue(await reader.ReadAsync());
}

[Test]
public async Task ReadStringValue_Int64_CommaErrors_MultipleAsync()
{
JsonTextReader reader = new JsonTextReader(new StringReader("['',,'']"));
await reader.ReadAsync();
await reader.ReadAsInt64Async();

await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () =>
{
await reader.ReadAsStringAsync();
}, "Unexpected character encountered while parsing value: ,. Path '[1]', line 1, position 5.");

Assert.AreEqual(string.Empty, await reader.ReadAsStringAsync());
Assert.IsTrue(await reader.ReadAsync());
}

[Test]
public async Task ReadStringValue_Numbers_NotStringAsync()
{
Expand Down
41 changes: 41 additions & 0 deletions Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ParseAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,47 @@ public async Task ParseIntegersAsync()
await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () => await reader.ReadAsInt32Async(), "Input string '-' is not a valid integer. Path '', line 1, position 1.");
}

[Test]
public async Task ParseIntegers64Async()
{
JsonTextReader reader = new JsonTextReader(new StringReader("1"));
Assert.AreEqual(1, await reader.ReadAsInt64Async());

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

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

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

reader = new JsonTextReader(new StringReader(long.MaxValue.ToString()));
Assert.AreEqual(long.MaxValue, await reader.ReadAsInt64Async());

reader = new JsonTextReader(new StringReader(long.MinValue.ToString()));
Assert.AreEqual(long.MinValue, await reader.ReadAsInt64Async());

reader = new JsonTextReader(new StringReader("9223372036854775899"));
await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () => await reader.ReadAsInt64Async(), "JSON integer 9223372036854775899 is too large or small for an Int64. Path '', line 1, position 19.");

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

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

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

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

reader = new JsonTextReader(new StringReader("-"));
await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () => await reader.ReadAsInt64Async(), "Input string '-' is not a valid integer. Path '', line 1, position 1.");
}


[Test]
public async Task ParseDecimalsAsync()
{
Expand Down
Loading