question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 (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:closed
  • Created a year ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, May 26, 2022

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 to ValidationFailure. 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.

1reaction
JeremySkinnercommented, Apr 21, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

System.Text.Json is unable to deserialize FluentValidation. ...
The deserialized2 variable contains a ValidationResult object with IsValid = false, and 1 Error. The serialization operation produces an ...
Read more >
System.Text.Json is unable to deserialize FluentValidation. ...
The deserialized2 variable contains a ValidationResult object with IsValid = false, and 1 Error. The serialization operation produces an ...
Read more >
System.Text.Json Deserialize Fails - net core
1 Answer 1 ... The default .NET core API serializes JSON as camel case, and the default System.Text.Json expects the JSON to match...
Read more >
Creating your first validator
The Validate method returns a ValidationResult object. This contains two properties: IsValid - a boolean that says whether the validation succeeded. Errors -...
Read more >
Validation and Error Handling
Normally FluentValidation only executes the matching rule set and ignores all other rules (whether they're in a rule set or not) and the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found