DataContractJsonSerializer parsing iso 8601 date
Use one string property for serialisation/deserialisation, and a separate, non-serialised property that converts it to a DateTime. Easier to see some sample code:
[DataContract]
public class LibraryBook
{
[DataMember(Name = "ReturnDate")]
// This can be private because it's only ever accessed by the serialiser.
private string FormattedReturnDate { get; set; }
// This attribute prevents the ReturnDate property from being serialised.
[IgnoreDataMember]
// This property is used by your code.
public DateTime ReturnDate
{
// Replace "o" with whichever DateTime format specifier you need.
// "o" gives you a round-trippable format which is ISO-8601-compatible.
get { return DateTime.ParseExact(FormattedReturnDate, "o", CultureInfo.InvariantCulture); }
set { FormattedReturnDate = value.ToString("o"); }
}
}
You could do the parsing in the setter of FormattedReturnDate instead, to allow it to fail earlier if a bad date is received.
Edited to include GôTô's suggestion to give the serialised DataMember the right name.