Skip to content

System.Text.Json is unable to deserialize FluentValidation.Results.ValidationResult #68280

@Arclight3

Description

@Arclight3

Description

System.Text.Json is unable to deserialize FluentValidation.Results.ValidationResult, while Newtonsoft.Json is able to do it.

Reproduction Steps

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.

Expected behavior

The Error properties should be deserialized.

Actual behavior

The Error properties are not deserialized.

Regression?

I am not sure. I only tested it in .NET 6 (LTS).

Known Workarounds

Using Newtonsoft Json instead...

Configuration

OS: Windows 10
.NET version: 6.0.202
Architecture: x64

Other information

I found this old issue on the FluentValidation repo: FluentValidation/FluentValidation#719.
They described there what was the issue when no library was able to deserialize from json to ValidationResult, but that issue was fixed and Newtonsoft Json works properly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions