Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Text.Json can't deserialize ISO dateTime to DateOnly #102594

Open
marcinjahn opened this issue May 23, 2024 · 4 comments
Open

System.Text.Json can't deserialize ISO dateTime to DateOnly #102594

marcinjahn opened this issue May 23, 2024 · 4 comments
Labels
area-System.Text.Json enhancement Product code improvement that does NOT require public API changes/additions
Milestone

Comments

@marcinjahn
Copy link

marcinjahn commented May 23, 2024

Description

It looks like System.Text.Json cannot deserialize from ISO 8601 datetime string to DateOnly, which I find odd, since DateOnly.Parse("2024-05-01T00:00:00") works perfectly fine.

Reproduction Steps

Here's the code:

record MyRecord(DateOnly Date);

var serialized = "{ \"Date\": \"2025-05-01T00:00:00\" }";

System.Text.Json.JsonSerializer.Deserialize<MyRecord>(serialized); // throws

Expected behavior

I'd expect no exception to be thrown and an isntance of MyRecord to be created.

Actual behavior

An exception is thrown:

The JSON value could not be converted to Submission#1+MyRecord. Path: $.Date | LineNumber: 0 | BytePositionInLine: 31.

Regression?

No response

Known Workarounds

Use DateTime instead of DateOnly.

Configuration

dotnet: 8.0.204
OS: Fedora 40
arch: x64

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 23, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@gregsdennis
Copy link
Contributor

gregsdennis commented May 23, 2024

Possible duplicate of #85545 (maybe just related)

@eiriktsarpalis
Copy link
Member

I believe this was a deliberate decision for the built-in to avoid loss of information (same is true for TimeOnly). I wouldn't be opposed to a PR that extends this to full ISO dates, if you're willing to put up a PR.

cc @Jozkee

@eiriktsarpalis eiriktsarpalis added enhancement Product code improvement that does NOT require public API changes/additions and removed untriaged New issue has not been triaged by the area owner labels May 23, 2024
@eiriktsarpalis eiriktsarpalis added this to the Future milestone May 23, 2024
@marcinjahn
Copy link
Author

I think it's expected that the time portion of the datetime string will be lost when you're deserializing to a DateOnly. It's kind of similar to how you might have a type:

record (int PropOne, int PropB);

and then you try to deserialize:

{
  "PropOne": 1,
  "PropTwo": 2,
  "PropThree": 3
}

The PropThree field gets lots, and it's expected.

I will try to prepare a PR for it. I will see if I manage to build it locally. If not, I guess I could rely on PR tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Text.Json enhancement Product code improvement that does NOT require public API changes/additions
Projects
None yet
Development

No branches or pull requests

3 participants