ErrorHandlingAttribute
November 14, 2024 ยท View on GitHub
This sample uses Argon.OnErrorAttribute to ignore the exception thrown setting the Roles property.
public class Employee :
IJsonOnSerializeError
{
public string Name { get; set; }
public int Age { get; set; }
public List<string> Roles
{
get
{
if (field == null)
{
throw new("Roles not loaded!");
}
return field;
}
set;
}
public string Title { get; set; }
public void OnSerializeError(object originalObject, string path, object member, Exception exception, Action markAsHandled) =>
markAsHandled();
}
var person = new Employee
{
Name = "George Michael Bluth",
Age = 16,
Roles = null,
Title = "Mister Manager"
};
var settings = new JsonSerializerSettings();
settings.AddInterfaceCallbacks();
var json = JsonConvert.SerializeObject(person, Formatting.Indented, settings);
Console.WriteLine(json);
// {
// "Name": "George Michael Bluth",
// "Age": 16,
// "Title": "Mister Manager"
// }