I'm using Json.NET to serialize an object that has an IEnumerable
of an enum and DateTime
. It's something like:
class Chart
{
// ...
public IEnumerable<int> YAxis { get; set; }
public IEnumerable<State> Data { get; set; }
public IEnumerable<DateTime> XAxis { get; set; }
}
But I need a custom JsonConverter
to make the enum serialize as string and to change the DateTime
string format.
I've tried using the JsonConverter
attribute as mentioned here for enum and a custom IsoDateTimeConverter
as done here:
[JsonConverter(typeof(StringEnumConverter))]
public IEnumerable<State> Data { get; set; }
[JsonConverter(typeof(MyDateTimeConverter))]
public IEnumerable<DateTime> XAxis { get; set; }
I was hoping it would work for an IEnumerable
too, but unsurprisingly it doesn't:
Unable to cast object of type 'WhereSelectArrayIterator`2[System.Int32,Model.State]' to type 'System.Enum'.
Is there any way to say that the JsonConverterAttribute
applies to each item and not on the enumerable itself?