How to change the default JSON DateTime
serialization/deserialization to a custom format using DataContractJsonSerializer
for all DateTime
properties in the object graph?
The Json.Net library deals with this but I can't use that in this project.
I tried IDataContractSurrogate
but I cannot access the value -> string conversion for DateTimes.
The model and expected JSON are:
[DataContract]
public class Client
{
[DataMember(Name = "id")]
public int Id {get; set; }
[DataMember(Name = "name")]
public string Name {get; set; }
[DataMember(Name = "contacts")]
public IList<Contact> Contacts {get; set; }
[DataMember(Name = "created")]
public DateTime Created {get; set; }
[DataMember(Name = "changed")]
public DateTime Changed {get; set; }
}
[DataContract]
public class Contact
{
[DataMember(Name = "name")]
public string Name {get; set; }
[DataMember(Name = "created")]
public DateTime Created {get; set; }
}
{
"id": 123,
"name": "Client Name",
"contacts": [
{
"name": "Contact Name",
"created": "2014-01-25 02:12:43"
}
],
"created": "2014-01-25 01:11:23"
"changed": "2014-01-25 03:22:41"
}