diff --git a/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs index aeb5e3d3..ee3be244 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs @@ -1026,6 +1026,21 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.AreEqual("Could not convert string to DateTime: 200NOTDATE. Path 'dateTime1', line 7, position 27.", errorMessages[2]); } + [Test] + public void HandleErrorInDictionaryObject() + { + var json = @"{ + model1: { String1: 's1', Int1: 'x' }, + model2: { String1: 's2', Int1: 2 } + }"; + var dictionary = JsonConvert.DeserializeObject>(json); + + Assert.AreEqual(1, dictionary.Count); + Assert.IsTrue(dictionary.ContainsKey("model2")); + Assert.AreEqual("s2", dictionary["model2"].String1); + Assert.AreEqual(2, dictionary["model2"].Int1); + } + private class DataModel { public string String1 { get; set; } @@ -1133,4 +1148,16 @@ namespace Newtonsoft.Json.Tests.Serialization { } } -} \ No newline at end of file + + /// + /// A dictionary that ignores deserialization errors and excludes bad items + /// + public class TolerantDictionary : Dictionary + { + [OnError] + public void OnDeserializationError(StreamingContext streamingContext, ErrorContext errorContext) + { + errorContext.Handled = true; + } + } +} diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index ce67dabe..83386ed4 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -1474,7 +1474,7 @@ namespace Newtonsoft.Json.Serialization if (IsErrorHandled(list, contract, errorPosition.Position, reader as IJsonLineInfo, reader.Path, ex)) { - HandleError(reader, true, initialDepth); + HandleError(reader, true, initialDepth + 1); if (previousErrorIndex != null && previousErrorIndex == errorPosition.Position) { @@ -1628,7 +1628,7 @@ namespace Newtonsoft.Json.Serialization if (IsErrorHandled(underlyingList, contract, errorPosition.Position, reader as IJsonLineInfo, reader.Path, ex)) { - HandleError(reader, true, initialDepth); + HandleError(reader, true, initialDepth + 1); if (previousErrorIndex != null && previousErrorIndex == errorPosition.Position) { @@ -2338,7 +2338,7 @@ namespace Newtonsoft.Json.Serialization { if (IsErrorHandled(newObject, contract, memberName, reader as IJsonLineInfo, reader.Path, ex)) { - HandleError(reader, true, initialDepth - 1); + HandleError(reader, true, initialDepth); } else { @@ -2535,7 +2535,7 @@ namespace Newtonsoft.Json.Serialization { reader.Skip(); - while (reader.Depth > (initialDepth + 1)) + while (reader.Depth > initialDepth) { if (!reader.Read()) { @@ -2545,4 +2545,4 @@ namespace Newtonsoft.Json.Serialization } } } -} \ No newline at end of file +}