I am receiving JSON data to my server with dates represented like this:
{ "startDate": { "d": "/Date(1346454000000)/" }, "slots": [ { "d": "/Date(1347058800000)/" }, { "d": "/Date(1347145200000)/" } ] } Its serialized to a simple object:
public class SlotsVm { public DateTime StartDate { get; set; } public DateTime[] Slots { get; set; } } Because the date format is strange, I had to write a custom JsonConverter to process it. To deserialize, I use code like this:
var slotsVm = JsonConvert.DeserializeObject<SlotsVm>(body, new CustomDateTimeConverter()); If possible, I would like the need for the converter to be defined on the SlotsVm class, rather than in the code that actually makes the conversion. This is possible for the startDate property using attributes:
[JsonConverter(typeof(CustomDateTimeConverter))] public DateTime StartDate { get; set; } but it is not possible for Slots, which is an array instead of a simple DateTime.
It would be best for me to be able to define the converters that the class needs on the class itself:
[JsonConverters(typeof(CustomDateTimeConverter), ...] public class PutDealVm { } but there doesn't seem to be a way to do this.
Can you think of a solution? Is there some way to define converters for a class that I have missed? Alternatively, is it possible to define the converter that an array should user for each of its elements?