Skip to content

A JsonConverter for System.Text.Json that enables setting custom serialization error messages.

License

Notifications You must be signed in to change notification settings

magyarandras/custom-json-errors

Repository files navigation

Custom JSON errors

A JsonConverter for System.Text.Json that enables setting custom serialization error messages.

Usage example:

public class Event
{
    [JsonConverterWithMessage("Invalid event date!")]
    public DateTime EventDate { get; set; }

}
string jsonString = @"{
	""EventDate"": ""Not a date"",
}";

try
{
    Event? evt = JsonSerializer.Deserialize<Event>(jsonString);

    if (evt != null)
    {
        Console.WriteLine(evt.EventDate);
    }
}
catch (JsonException ex)
{
    //Should be "Invalid event date!"
    Console.WriteLine(ex.Message);
}

About

A JsonConverter for System.Text.Json that enables setting custom serialization error messages.

Topics

Resources

License

Stars

Watchers

Forks

Languages