I need to deserialize a class with different types (int, float, string, guid) of properties. I am hoping to find a way to do this so that:
- if a value does not match it's expected type an error would be added to an error list
- and continue with the deserialization to have the full list of errors rather than just the first one
Example
public record Row()
[JsonProperty("columnName")]
public string? ColumnName { get; set; }
[JsonProperty("entityCode")]
public Guid? EntityCode { get; set; }
[JsonProperty("numberValue")]
public float? NumberValue { get; set; }
[JsonProperty("integerValue")]
public int? IntegerValue { get; set; }
[JsonProperty("booleanValue")]
public bool? BooleanValue { get; set; }
Object example
"Row": [
{
"columnName": "category",
"entityCode": "not a guid"
},
{
"columnName": "numberValue",
"numberValue": "not a number"
},
{
"columnName": "value",
"stringValue": 104.28398
},
{
"columnName": "value",
"booleanValue": "not a bool"
}
],
And then I need to defserialize the object above
JsonConvert.DeserializeObject<IList<Row>>
But instead of instantly jumping to an exception with one error I need to know all the properties that have wrong types.
Can this be done?
did not provided correct class structure. you can do with custom serializing settings
results:
UPDATE: this is definetly XY problem