Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json.NET Serializing / Deserializing Array of Datetimes with Custom Format

I am trying to apply a custom datetime format to an array of datetime values. I know you can use IsoDateTimeConverter to accomplish this for single values e.g.

class CustomDateTimeConverter : IsoDateTimeConverter
{
    public CustomDateTimeConverter()
    {
        base.DateTimeFormat = "yyyy-MM-dd";
    }
}

class ReturnObjectA 
{
    [JsonConverter(typeof(CustomDateTimeConverter))]
    public DateTime ReturnDate { get; set;}
}

But how do apply the same thing to an array of datetimes? e.g. The following does not work.

class ReturnObjectA 
{
    [JsonConverter(typeof(CustomDateTimeConverter))]
    public DateTime[] ReturnDate { get; set;}
}
like image 278
n00j Avatar asked Oct 14 '25 09:10

n00j


1 Answers

JsonProperty is what you're looking for:

class ReturnObjectA
{
    [JsonProperty(ItemConverterType = typeof(CustomDateTimeConverter))]
    public DateTime[] ReturnDate { get; set; }
}
like image 56
Jon Avatar answered Oct 16 '25 23:10

Jon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!