I need a way to ignore rows / items that fail JsonProperty (throws an error) within a large list that's provided to newtonsoft JsonCovert as JSON.
Currently utilising this question (and answer) as a based to handle the errors which was working well until a couple cases.
Using this flow where the error context is not handled:
var currentError = errorArgs.ErrorContext.Error.Message;
errorArgs.ErrorContext.Handled = false;
It doesn't return any items, even those that do pass validation in the string
Using this flow however:
var currentError = errorArgs.ErrorContext.Error.Message;
errorArgs.ErrorContext.Handled = true;
All the items are serialized, the error is thrown on those properties that have
JsonProperty("property_name", Required = Required.Always, NullValueHandling = NullValueHandling.Ignore)
or JsonRequired
and they're still included in the list, which causes errors down the line when this list is being bulk fed into a database.
Ultimately, I would like to know if there is a way to not include items in the resulting list that have failed, rather than taking all of them or none at all
The fix that I was able to figure out was to:
Get the errorArgs.ErrorContent.path in the format "[index]" and strip out the '[' and ']', parse this to an integer, store it in a list.
Set error handled to true for all cases, and then once the deserialization had completed I then used this list of integers to remove the offending items