I am using the Newtonsoft JSON library to deserialize a response from a web service. The problem is that some of the fields contain invalid values. For example, one field in one record contained a “T” for the field, which should be numeric. What I would like to do is to have values for fields that are invalid, zero, or some other default value. All my properties are defined as nullable, so it’s ok if they default to null.
Is there any way to do this? I tried to create custom JsonConverters, but I would prefer not to define JsonConverter for each type. If possible, I would like to set all fields to null if the value for this property is not valid (for example, "T" for a numeric type).
I looked at the OnError event handler, but this seems to discard the entire error record. I do not want to discard records. I just would like the value of the invalid properties to be null.
Is it possible? I searched a lot for answers, and I did not find another question that tried to do this, but please let me know if I missed an existing question.
Thank you for your help.
source
share