suppose I have the following DTO:
public class FooDto
{
public string Bar { get; set; }
public DayOfWeek[] Days { get; set; }
}
In order to comply with the corresponding API, this DTO needs to serialize to json like this:
{
"bar": "someString",
"days": ["Monday", "Friday", "Sunday"]
}
Without any further actions the same DTO would serialize like this:
{
"bar": "someString",
"days": [1, 5, 0]
}
My Question:
Is there a way to use System.Text.Json in order to serialize an array of a third-party Enums like System.DayOfWeek as an array of it's string values? I am specifically not looking for a solution using json.net (Newtonsoft).
My Ideas that didn't work out:
- Using
[JsonConverter(typeof(JsonStringEnumConverter))]does not work on arrays. - In this case it's recommended to put the annotation directly above the Enum itself. This isn't possible since it's a thrid-party Enum.
- I can't simply create a "proxy" for
System.DayOfWeeksince Enums don't support inheritance. - I could create my own
DayOfWeekDtoand write a mapper fromSystem.DayOfWeektoDayOfWeekDtoand vice versa, but this "solution" doesn't seem elegant at all.
Ideas for simple solutions that don't include Newtonsoft would be very welcome :)