1

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?

1 Answer 1

1

What about something like this? I appreciate that this may not directly answer your question, but I was too lazy to recreate the CustomDateTimeConverter class, so came up with this instead, which is arguably simpler, as there is no need for a custom converter.

string json = "{\"startDate\": {\"d\": \"/Date(1346454000000)/\"},\"slots\": [{\"d\": \"/Date(1347058800000)/\"},{\"d\": \"/Date(1347145200000)/\"}]}"; SlotsVmDeserialized slotsVmDeserialized = JsonConvert.DeserializeObject<SlotsVmDeserialized>(json); SlotsVm slotsVm = new SlotsVm() { Slots = slotsVmDeserialized.Slots.Select(d => d.Date), StartDate = slotsVmDeserialized.StartDate.Date }; .... public class SlotsVm { public DateTime StartDate { get; set; } public IEnumerable<DateTime> Slots { get; set; } } public class SlotsVmDeserialized { public DateObject StartDate { get; set; } public IEnumerable<DateObject> Slots { get; set; } } public class DateObject { [JsonProperty("d")] public DateTime Date { get; set; } } 

What I've done here is firstly deserialized to the SlotsVmDeserialized class, which is used to hold a single DataObject and an IEnumerable of them. The DateObject class is essentially my alternative/workaround to the custom converter. I could have done all this without any attributes by removing [JsonProperty("d")] and renaming Data to D.

Once deserialized, I then create a new SlotsVm object based on the properties of the SlotsVmDeserialized object.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a nice trick. It doesn't quite solve the main problem, which is to have the deserialization process fairly transparent to the caller, but I think I can use the idea of a custom DateObject class. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.