-Fixed not erroring when reading undefined for nullable long

This commit is contained in:
James Newton-King
2017-07-23 13:50:49 +12:00
parent ad1a13728a
commit 403470d59a
8 changed files with 175 additions and 1 deletions
@@ -56,6 +56,38 @@ namespace Newtonsoft.Json.Tests.JsonTextReaderTests
#endif
public class ReadAsyncTests : TestFixtureBase
{
[Test]
public async Task ReadMissingInt64()
{
string json = "{ A: \"\", B: 1, C: , D: 1.23, E: 3.45, F: null }";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
await reader.ReadAsync();
await reader.ReadAsync();
await reader.ReadAsync();
await reader.ReadAsync();
await reader.ReadAsync();
await reader.ReadAsync();
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("C", reader.Value);
await reader.ReadAsync();
Assert.AreEqual(JsonToken.Undefined, reader.TokenType);
Assert.AreEqual(null, reader.Value);
}
[Test]
public async Task ReadAsInt32AsyncWithUndefined()
{
await ExceptionAssert.ThrowsAsync<JsonReaderException>(async () =>
{
JsonTextReader reader = new JsonTextReader(new StringReader("undefined"));
await reader.ReadAsInt32Async();
},
"Unexpected character encountered while parsing value: u. Path '', line 1, position 1.");
}
#if !(PORTABLE || PORTABLE40 || NET35 || NET20)
[Test]
public async Task ReadAsBooleanAsync()
@@ -61,6 +61,38 @@ namespace Newtonsoft.Json.Tests.JsonTextReaderTests
#endif
public class ReadTests : TestFixtureBase
{
[Test]
public void ReadMissingInt64()
{
string json = "{ A: \"\", B: 1, C: , D: 1.23, E: 3.45, F: null }";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
reader.Read();
reader.Read();
reader.Read();
reader.Read();
reader.Read();
reader.Read();
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("C", reader.Value);
reader.Read();
Assert.AreEqual(JsonToken.Undefined, reader.TokenType);
Assert.AreEqual(null, reader.Value);
}
[Test]
public void ReadAsInt32WithUndefined()
{
ExceptionAssert.Throws<JsonReaderException>(() =>
{
JsonTextReader reader = new JsonTextReader(new StringReader("undefined"));
reader.ReadAsInt32();
},
"Unexpected character encountered while parsing value: u. Path '', line 1, position 1.");
}
#if !(PORTABLE || PORTABLE40 || NET35 || NET20)
[Test]
public void ReadAsBoolean()
@@ -68,6 +68,30 @@ namespace Newtonsoft.Json.Tests.Linq
}
#endif
[Test]
public void ToObjectFromGuidToString()
{
JValue token = new JValue(new Guid("91274484-3b20-48b4-9d18-7d936b2cb88f"));
string value = token.ToObject<string>();
Assert.AreEqual("91274484-3b20-48b4-9d18-7d936b2cb88f", value);
}
[Test]
public void ToObjectFromIntegerToString()
{
JValue token = new JValue(1234);
string value = token.ToObject<string>();
Assert.AreEqual("1234", value);
}
[Test]
public void ToObjectFromStringToInteger()
{
JValue token = new JValue("1234");
int value = token.ToObject<int>();
Assert.AreEqual(1234, value);
}
[Test]
public void FromObjectGuid()
{
@@ -10141,6 +10141,46 @@ This is just junk, though.";
Assert.AreEqual(dt, c2.NormalDate);
Assert.AreEqual(dt, c2.JavaScriptDate);
}
[Test]
public void StringEmptyValue()
{
ExceptionAssert.Throws<JsonReaderException>(
() => JsonConvert.DeserializeObject<EmptyJsonValueTestClass>("{ A: , B: 1, C: 123, D: 1.23, E: 3.45, F: null }"),
"Unexpected character encountered while parsing value: ,. Path 'A', line 1, position 6.");
}
[Test]
public void NullableIntEmptyValue()
{
ExceptionAssert.Throws<JsonReaderException>(
() => JsonConvert.DeserializeObject<EmptyJsonValueTestClass>("{ A: \"\", B: , C: 123, D: 1.23, E: 3.45, F: null }"),
"Unexpected character encountered while parsing value: ,. Path 'B', line 1, position 13.");
}
[Test]
public void NullableLongEmptyValue()
{
ExceptionAssert.Throws<JsonReaderException>(
() => JsonConvert.DeserializeObject<EmptyJsonValueTestClass>("{ A: \"\", B: 1, C: , D: 1.23, E: 3.45, F: null }"),
"An undefined token is not a valid System.Nullable`1[System.Int64]. Path 'C', line 1, position 18.");
}
[Test]
public void NullableDecimalEmptyValue()
{
ExceptionAssert.Throws<JsonReaderException>(
() => JsonConvert.DeserializeObject<EmptyJsonValueTestClass>("{ A: \"\", B: 1, C: 123, D: , E: 3.45, F: null }"),
"Unexpected character encountered while parsing value: ,. Path 'D', line 1, position 27.");
}
[Test]
public void NullableDoubleEmptyValue()
{
ExceptionAssert.Throws<JsonReaderException>(
() => JsonConvert.DeserializeObject<EmptyJsonValueTestClass>("{ A: \"\", B: 1, C: 123, D: 1.23, E: , F: null }"),
"Unexpected character encountered while parsing value: ,. Path 'E', line 1, position 36.");
}
}
public class JsonPropertyConverterTestClass
@@ -0,0 +1,37 @@
#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
namespace Newtonsoft.Json.Tests.TestObjects
{
public class EmptyJsonValueTestClass
{
public string A { get; set; }
public int? B { get; set; }
public long? C { get; set; }
public decimal? D { get; set; }
public double? E { get; set; }
public string F { get; set; }
}
}
+7
View File
@@ -1190,6 +1190,13 @@ namespace Newtonsoft.Json
case ReadType.ReadAsInt32:
ReadAsInt32();
break;
case ReadType.ReadAsInt64:
bool result = ReadAndMoveToContent();
if (TokenType == JsonToken.Undefined)
{
throw JsonReaderException.Create(this, "An undefined token is not a valid {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
}
return result;
case ReadType.ReadAsDecimal:
ReadAsDecimal();
break;
+1
View File
@@ -38,6 +38,7 @@ namespace Newtonsoft.Json
{
Read,
ReadAsInt32,
ReadAsInt64,
ReadAsBytes,
ReadAsString,
ReadAsDecimal,
@@ -69,7 +69,8 @@ namespace Newtonsoft.Json.Serialization
[typeof(DateTimeOffset)] = ReadType.ReadAsDateTimeOffset,
#endif
[typeof(float)] = ReadType.ReadAsDouble,
[typeof(double)] = ReadType.ReadAsDouble
[typeof(double)] = ReadType.ReadAsDouble,
[typeof(long)] = ReadType.ReadAsInt64
};
}
}