Custom json serialization for each item in IEnumerable [duplicate]
Solution 1:
Turns out that for enumerables you have to use the JsonPropertyAttribute
and the ItemConverterType
property as follows:
[JsonProperty(ItemConverterType = typeof(StringEnumConverter))]
public IEnumerable<State> Data { get; set; }
[JsonProperty(ItemConverterType = typeof(MyDateTimeConverter))]
public IEnumerable<DateTime> XAxis { get; set; }
This is mentioned in the documentation as:
To apply a JsonConverter to the items in a collection, use either JsonArrayAttribute, JsonDictionaryAttribute or JsonPropertyAttribute and set the ItemConverterType property to the converter type you want to use.
You might be confused with JsonArrayAttribute
, but it
cannot target a property.