From 12b38ccb19c4734028e85ebe8b33df73d61bd320 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sun, 11 Sep 2016 19:37:44 +1200 Subject: [PATCH] -Fixed error handling skipping reading the next object value --- .../SerializationErrorHandlingTests.cs | 37 +++++++++++++++++++ .../JsonSerializerInternalReader.cs | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs index a3099970..1491c0ac 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs @@ -915,6 +915,43 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.AreEqual(string.Empty, result); } + + [Test] + public void IntegerToLarge_ReadNextValue() + { + IList errorMessages = new List(); + + JsonReader reader = new JsonTextReader(new StringReader("{\"string1\":\"blah\",\"int1\":2147483648,\"string2\":\"also blah\",\"int2\":2147483648,\"string3\":\"more blah\"}")); + JsonSerializerSettings settings = new JsonSerializerSettings(); + settings.Error = (sender, args) => + { + errorMessages.Add(args.ErrorContext.Error.Message); + args.ErrorContext.Handled = true; + }; + JsonSerializer serializer = JsonSerializer.Create(settings); + + DataModel data = new DataModel(); + serializer.Populate(reader, data); + + Assert.AreEqual("blah", data.String1); + Assert.AreEqual(0, data.Int1); + Assert.AreEqual("also blah", data.String2); + Assert.AreEqual(0, data.Int2); + Assert.AreEqual("more blah", data.String3); + + Assert.AreEqual(2, errorMessages.Count); + Assert.AreEqual("JSON integer 2147483648 is too large or small for an Int32. Path 'int1', line 1, position 35.", errorMessages[0]); + Assert.AreEqual("JSON integer 2147483648 is too large or small for an Int32. Path 'int2', line 1, position 75.", errorMessages[1]); + } + + private class DataModel + { + public string String1 { get; set; } + public int Int1 { get; set; } + public string String2 { get; set; } + public int Int2 { get; set; } + public string String3 { get; set; } + } } internal interface IErrorPerson2 diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 653810b3..e04288b1 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -2382,7 +2382,7 @@ namespace Newtonsoft.Json.Serialization { if (IsErrorHandled(newObject, contract, memberName, reader as IJsonLineInfo, reader.Path, ex)) { - HandleError(reader, true, initialDepth); + HandleError(reader, false, initialDepth); } else {