quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

Enhancement: C# Use the right exception for System.Text.Json converters

Open sommmen opened this issue 2 years ago • 0 comments

Please see: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/converters-how-to?pivots=dotnet-7-0#error-handling

Error handling

The serializer provides special handling for exception types JsonException and NotSupportedException.

When to throw which exception type

When the JSON payload contains tokens that are not valid for the type being deserialized, throw a JsonException.

When you want to disallow certain types, throw a NotSupportedException. This exception is what the serializer automatically throws for types that are not supported. For example, System.Type is not supported for security reasons, so an attempt to deserialize it results in a NotSupportedException.

You can throw other exceptions as needed, but they don't automatically include JSON path information.

Which reccomends throwing JsonException or NotSupportedException (without arguments). The proplem now is that when conversion fails, no path information is shown so its hard to see what property caused the faulty conversion. Using the exceptions described above would fix this.

sommmen avatar Aug 10 '23 11:08 sommmen