-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
FluentValidation 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 (dotnet/runtime#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.