diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadAsyncTests.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadAsyncTests.cs index 42349002..19b140be 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadAsyncTests.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadAsyncTests.cs @@ -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(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() diff --git a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadTests.cs b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadTests.cs index 55539c0d..3a497f7b 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadTests.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextReaderTests/ReadTests.cs @@ -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(() => + { + 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() diff --git a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs index b1fc6395..20b4ed19 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs @@ -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(); + Assert.AreEqual("91274484-3b20-48b4-9d18-7d936b2cb88f", value); + } + + [Test] + public void ToObjectFromIntegerToString() + { + JValue token = new JValue(1234); + string value = token.ToObject(); + Assert.AreEqual("1234", value); + } + + [Test] + public void ToObjectFromStringToInteger() + { + JValue token = new JValue("1234"); + int value = token.ToObject(); + Assert.AreEqual(1234, value); + } + [Test] public void FromObjectGuid() { diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 1d4fb837..89f7ad90 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -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( + () => JsonConvert.DeserializeObject("{ 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( + () => JsonConvert.DeserializeObject("{ 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( + () => JsonConvert.DeserializeObject("{ 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( + () => JsonConvert.DeserializeObject("{ 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( + () => JsonConvert.DeserializeObject("{ 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 diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/EmptyJsonValueTestClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/EmptyJsonValueTestClass.cs new file mode 100644 index 00000000..59c3472c --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/TestObjects/EmptyJsonValueTestClass.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs index edc23360..debd1d86 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -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; diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs index e85d69f0..e04aa0f8 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.cs @@ -38,6 +38,7 @@ namespace Newtonsoft.Json { Read, ReadAsInt32, + ReadAsInt64, ReadAsBytes, ReadAsString, ReadAsDecimal, diff --git a/Src/Newtonsoft.Json/Serialization/JsonPrimitiveContract.cs b/Src/Newtonsoft.Json/Serialization/JsonPrimitiveContract.cs index ab5cba39..9c32f960 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonPrimitiveContract.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonPrimitiveContract.cs @@ -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 }; } } \ No newline at end of file