Skip to content

JsonSerializer does not support parsing DateTime value '0001-01-01T00:00:00+01:00' #70547

Open

Description

@aguacongas

Description

JsonSerializer throw the exception :

System.Text.Json.JsonException: 'The JSON value could not be converted to System.DateTime. Path: $

On a date it serialize itself when the value is '0001-01-01T00:00:00+01:00'

Reproduction Steps

  • Set your time zone to (UTC+01:00) Amsterdam, Berlin. Bern, Rome, Stockholm, Vienna
  • Launch the test :
[Fact]
public void JsonSerializerIssue()
{
    var expected = new DateTime(1, 1, 1, 0, 0, 0, DateTimeKind.Local);
    var serialized = JsonSerializer.Serialize(expected);
    var result = JsonSerializer.Deserialize<DateTime>(serialized);

    Assert.Equal(expected, result);
}

Expected behavior

No exception

Actual behavior

The exception

System.Text.Json.JsonException: 'The JSON value could not be converted to System.DateTime. Path: $

is thrown

Regression?

No response

Known Workarounds

We can workaround with a JsonConverter<DateTime> but it's ugly and as the serialization do not throw an exception the deserialization should.

class DateTimeConverterUsingDateTimeParse : JsonConverter<DateTime>
{
    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
        writer.WriteStringValue(value.ToString("s"));                
    }
}

Configuration

  • Which version of .NET is the code running on? netcoreapp3.1
  • What OS and version, and what distro if applicable? all, I guess
  • What is the architecture (x64, x86, ARM, ARM64)? x64
  • Do you know whether it is specific to that configuration? I don't think so

Other information

No response

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jun 10, 2022
  2. eiriktsarpalis commented on Jun 10, 2022

    @eiriktsarpalis
    Member

    Fundamentally, I think the issue is that 0001-01-01T00:00:00+01:00 is an invalid ISO-8601 date/time string. If I try to parse it using DateTimeOffset.Parse I get the following error:

    The UTC representation of the date '0001-01-01T00:00:00+01:00' falls outside the year range 1-9999.
    

    And the same error surfaces if I try to convert you DateTime value to DateTimeOffset. I think the problem lies with the serializer, given that it serializes using ISO-8601 it should simply reject that particular value.

    cc @tarekgh

  3. added
    enhancementProduct code improvement that does NOT require public API changes/additions
    help wanted[up-for-grabs] Good issue for external contributors
    and removed
    untriagedNew issue has not been triaged by the area owner
    on Jun 10, 2022
  4. added this to the Future milestone on Jun 10, 2022
  5. Clockwork-Muse commented on Jun 10, 2022

    @Clockwork-Muse
    Contributor

    is an invalid ISO-8601 date/time string.

    nit: it's a valid date/time string, it's just outside the range supported by DateTime

    That aside, if you're attempting to create an invalid date for "not a value"/"not passed" semantics, you should be passing null, or a UTC timestamp, not one with an offset. (That's beside the question of whether you should be using DateTime at all, instead of DateTimeOffset or NodaTime, but that's a more involved analysis)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.JsonenhancementProduct code improvement that does NOT require public API changes/additionshelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions