Your StartDate
and EndDate
properties are not in ISO 8601-1:2019 format, which would look like "2021-02-02T18:07:33Z"
instead of "Tue, 02 Feb 2021 18:07:33 GMT"
. From DateTime and DateTimeOffset support in System.Text.Json:
The JsonSerializer, Utf8JsonReader, Utf8JsonWriter, and JsonElement types parse and write DateTime and DateTimeOffset text representations according to the extended profile of the ISO 8601-1:2019 format; for example, 2019-07-26T16:59:57-05:00.
...
With default options, input DateTime and DateTimeOffset text representations must conform to the extended ISO 8601-1:2019 profile. Attempting to deserialize representations that don't conform to the profile will cause JsonSerializer to throw a JsonException.
Thus you will need to create a custom JsonConverter<DateTime?>
along the lines of DateTimeConverterUsingDateTimeParse
that Microsoft shows in Custom support for DateTime and DateTimeOffset such as:
public class CustomDateTimeConverter : JsonConverter<DateTime?>
{
//const string Format = "Tue, 02 Feb 2021 18:07:33 GMT";
const string Format = "dddd, dd MMM yyyy hh:mm:ss";
// Adapted from https://learn.microsoft.com/en-us/dotnet/standard/datetime/system-text-json-support#using-datetimeoffsetparse-and-datetimeoffsettostring
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
DateTime.Parse(reader.GetString(), CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options) =>
writer.WriteStringValue(value?.ToUniversalTime().ToString(Format, CultureInfo.InvariantCulture)+" GMT");
}
You can then add the converter to JsonSerializerOptions.Converters
or apply it directly to the model like so:
public class FilingSearchCriteria
{
public int? FilerId { get; set; }
public int? TypeId { get; set; }
[JsonConverter(typeof(CustomDateTimeConverter))]
public DateTime? StartDate { get; set; }
[JsonConverter(typeof(CustomDateTimeConverter))]
public DateTime? EndDate { get; set; }
// Remainder unchanged
Notes:
System.Text.Json seems to require distinct JsonConverter<T>
converters for DateTime?
and DateTime
-- and generally for nullables and their underlying types.
DateTime.Parse()
apparently does not support parsing multiple names time zones so if you are receiving DateTime
strings with a mixture of different time zones, say both "GMT"
and "EDT"
, you will need to write a more complex converter.
My converter assumes you want your dates and times adjusted to universal. You can modify that if it is not desired.
Demo fiddle here.