Update initialDepth for error handling (#1550)
* Fix target depth when skipping over handled errors Keep reading until the reader reaches the initial depth rather than initial depth + 1 * Fix the initialDepth when handling errors in lists The initialDepth should be +1 when handling deserialization errors in lists and arrays in order to not skip subsequent items * Add unit tests for dictionary error handling Ignoring errors in dictionary object by completely removing the bad items from the dictionary
This commit is contained in:
committed by
James Newton-King
parent
69640e88d8
commit
e5e901c6d0
@@ -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<TolerantDictionary<string, DataModel>>(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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A dictionary that ignores deserialization errors and excludes bad items
|
||||
/// </summary>
|
||||
public class TolerantDictionary<TKey, TValue> : Dictionary<TKey, TValue>
|
||||
{
|
||||
[OnError]
|
||||
public void OnDeserializationError(StreamingContext streamingContext, ErrorContext errorContext)
|
||||
{
|
||||
errorContext.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user