I was reading this MSDocs article about DateTime-related format support https://learn.microsoft.com/en-us/dotnet/standard/datetime/system-text-json-support#support-for-the-iso-8601-12019-format
And I was trying to play around just to figure out that the default configuration wasn't working as expected or I must be missing something out.
I mean:
DateTimeOffset.Parse("2021-03-17T12:03:14+0000");
Works just fine.
BUT
JsonSerializer.Deserialize<TestType>(@"{ ""CreationDate"": ""2021-03-17T12:03:14+0000"" }");
Doesn't.
Example:
using System;
using System.Text.Json;
using System.Threading.Tasks;
namespace CSharpPlayground
{
public record TestType(DateTimeOffset CreationDate);
public static class Program
{
public static void Main()
{
var dto = DateTimeOffset.Parse("2021-03-17T12:03:14+0000");
Console.WriteLine(dto);
var testType = JsonSerializer.Deserialize<TestType>(@"{ ""CreationDate"": ""2021-03-17T12:03:14+0000"" }");
Console.WriteLine(testType.CreationDate);
}
}
}
The exception below is thrown:
System.Text.Json.JsonException: The JSON value could not be converted to CSharpPlayground.TestType. Path: $.CreationDate | LineNumber: 0 | BytePositionInLine: 44.
---> System.FormatException: The JSON value is not in a supported DateTimeOffset format.