diff --git a/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs index f0f7e0a7..97b6227b 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/SerializationErrorHandlingTests.cs @@ -52,6 +52,54 @@ namespace Newtonsoft.Json.Tests.Serialization [TestFixture] public class SerializationErrorHandlingTests : TestFixtureBase { + [Test] + public void ErrorHandlingMetadata() + { + List errors = new List(); + + AAA a2 = JsonConvert.DeserializeObject(@"{""MyTest"":{""$type"":"".JsonTest+MyTest2, ""}}", new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.Auto, + Error = (object sender, Json.Serialization.ErrorEventArgs e) => + { + errors.Add(e.ErrorContext.Error); + e.ErrorContext.Handled = true; + } + }); + + Assert.IsNotNull(a2); + Assert.AreEqual(1, errors.Count); + Assert.AreEqual("Error resolving type specified in JSON '.JsonTest+MyTest2, '. Path 'MyTest.$type', line 1, position 61.", errors[0].Message); + } + + [Test] + public void ErrorHandlingMetadata_TopLevel() + { + List errors = new List(); + + JObject a2 = (JObject)JsonConvert.DeserializeObject(@"{""$type"":"".JsonTest+MyTest2, ""}", new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.Auto, + Error = (object sender, Json.Serialization.ErrorEventArgs e) => + { + errors.Add(e.ErrorContext.Error); + e.ErrorContext.Handled = true; + } + }); + + Assert.IsNull(a2); + Assert.AreEqual(1, errors.Count); + Assert.AreEqual("Error resolving type specified in JSON '.JsonTest+MyTest2, '. Path '$type', line 1, position 51.", errors[0].Message); + } + + public class AAA + { + public ITest MyTest { get; set; } + } + + public interface ITest { } + public class MyTest : ITest { } + public class MyClass1 { [JsonProperty("myint")] diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 991ccb70..0c64e0f2 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -2408,7 +2408,7 @@ namespace Newtonsoft.Json.Serialization { if (IsErrorHandled(newObject, contract, memberName, reader as IJsonLineInfo, reader.Path, ex)) { - HandleError(reader, true, initialDepth); + HandleError(reader, true, initialDepth - 1); } else {