System.Text.Json is unable to deserialize FluentValidation.Results.ValidationResult
See original GitHub issueFluentValidation version
10.4.0
ASP.NET version
6.0.202
Summary
System.Text.Json is unable to deserialize FluentValidation.Results.ValidationResult, while Newtonsoft.Json is able to do it.
I filled an issue on dotnet runtime repo (https://github.com/dotnet/runtime/issues/68280) but they suggested to open one here because it might be that FluentValidation relies on conventions not supported by System.Text.Json or that it implements a converter only for Json.NET.
Steps to Reproduce
Check this code in .NET 6:
var validationResult = new ValidationResult
{
Errors = { new ValidationFailure("MyProperty", "Invalid MyProperty") }
};
// System.Text.Json
var serialized = JsonSerializer.Serialize(validationResult);
var deserialized = JsonSerializer.Deserialize<ValidationResult>(serialized);
// Newtonsoft.Json
var serialized2 = JsonConvert.SerializeObject(validationResult, Formatting.Indented);
var deserialized2 = JsonConvert.DeserializeObject<ValidationResult>(serialized2);
The deserialized variable contains a ValidationResult object with IsValid = true, and 0 Errors. The deserialized2 variable contains a ValidationResult object with IsValid = false, and 1 Error.
The serialization operation produces an identical json string for both libraries, but the System.Text.Json fails to deserialize it back to a ValidationResult object.
Issue Analytics
- State:
- Created a year ago
- Comments:12 (9 by maintainers)
Top GitHub Comments
I’ve had some time to think about this further and I think I’ve decided to opt for adding public setters to
ValidationResult
and a public parameterless constructor toValidationFailure
. This seems like the simplest option as it doesn’t require adding an explicit reference to System.Text.Json (which I want to avoid), nor does it require end users to define their own type converters.The setter will preserve the same behaviour as the existing constructor (copies the list of failures and removes nulls).
This will go into 11.0.2 which I’m aiming to release tomorrow.
I can’t really see what the issue is here. We don’t do anything special or provide a converter or anything like that for JSON.NET. If it works with JSON.NET then I’d expect it to work with System.Text.Json too…
I would’ve suggested that you report it to them, but I see you already did and they closed the issue 🤷 I’m not sure what conventions they’d be referring to, and I don’t really know why we’d need to provide a JsonConverter for what is a pretty simple type, but I’m probably missing something.
I’m not really able to help further I’m afraid - it really needs someone who knows about System.Text.Json to investigate, which isn’t something I have any experience with. I’ll tag this as Help Wanted for now and hopefully someone will be able to pick it up.