Skip to content

Add JsonConstructor attributes to Date and Time for System.Text.Json deserialization#641

Open
Copilot wants to merge 4 commits intomainfrom
copilot/fix-json-deserialization-date-time
Open

Add JsonConstructor attributes to Date and Time for System.Text.Json deserialization#641
Copilot wants to merge 4 commits intomainfrom
copilot/fix-json-deserialization-date-time

Conversation

Copy link
Contributor

Copilot AI commented Feb 4, 2026

Fix Date and Time deserialization with System.Text.Json ✅

Plan:

  • Add [JsonConstructor] attribute to Date(int year, int month, int day) constructor
  • Add [JsonConstructor] attribute to Time(int hour, int minute, int second) constructor
  • Create DateTests.cs with System.Text.Json deserialization tests
  • Add System.Text.Json deserialization tests to TimeTests.cs
  • Build and test to validate the changes
  • Request code review
  • Run security scan
  • Run dotnet format to fix whitespace formatting

Summary

Successfully fixed the deserialization issue with Date and Time types when using System.Text.Json.JsonSerializer.

Changes Made:

  1. Modified Microsoft.Kiota.Abstractions.csproj: Added System.Text.Json package reference (version 6.0.10+) for netstandard2.0, netstandard2.1, and net5.0 targets
  2. Modified Date.cs: Added using System.Text.Json.Serialization; and [JsonConstructor] attribute to the Date(int year, int month, int day) constructor
  3. Modified Time.cs: Added using System.Text.Json.Serialization; and [JsonConstructor] attribute to the Time(int hour, int minute, int second) constructor
  4. Created DateTests.cs: Added 4 comprehensive tests for Date serialization, deserialization, round-trip, and ToString
  5. Modified TimeTests.cs: Added 3 new tests for Time serialization, deserialization, and round-trip
  6. Applied code formatting: Ran dotnet format to fix whitespace formatting in DateTests.cs

Test Results:

  • ✅ All tests passing: 178/178 tests in abstractions project
  • ✅ 9 Date and Time specific tests all pass
  • ✅ Manual verification confirms deserialized objects contain correct values instead of default values
  • ✅ Code review: No issues found
  • ✅ Security scan: No vulnerabilities found
  • ✅ Code formatting: Passes dotnet format verification

Security Summary:

No security vulnerabilities were discovered or introduced by this change.

Original prompt

This section details on the original issue you should resolve

<issue_title>System.Text.Json.JsonSerializer cannot deserialize Date and Time types using default converters</issue_title>
<issue_description>Hello!

The default JSON converters in System.Text.Json do not work with the Date or Time types. These types are serialized as objects containing the read-only properties. Those properties are not set when deserializing, so they get the default values. Same issue applies with Newtonsoft.Json.

This is a problem when using third-party libraries that can't use KiotaJsonSerializer or the extension methods. My specific use case is using Temporal, which serializes values returned from activities and deserializes them later. I had an activity that returned a Kiota-generated model with a date, but the date later deserialized to 0001-01-01. This is easy to fix by creating a type that uses DateOnly or registering a new JsonConverter, but the bug confused me for a while.

This was briefly mentioned in #282 which was closed due to no response. That issue also mentions serializing enums, which I haven't tried.

Demo: (.NET 9.0.10 using Microsoft.Kiota.Abstractions v1.20.1)

var date = new Date(DateTime.Now);

var dateSerialized = JsonSerializer.Serialize(date);
// {
//    "DateTime": "2025-10-24T10:18:54.5003283-05:00",
//    "Year": 2025,
//    "Month": 10,
//    "Day": 24
// }

var dateDeserialized = JsonSerializer.Deserialize<Date>(dateSerialized);
// {
//    DateTime = 1/1/0001 12:00:00 AM
//    Year = 1
//    Month = 1
//    Day = 1
// }

var time = new Time(DateTime.Now);

var timeSerialized = JsonSerializer.Serialize(time);
// {
//    "DateTime": "2025-10-24T10:18:54.5090402-05:00",
//    "Hour": 10,
//    "Minute": 18,
//    "Second": 54
// }

var timeDeserialized = JsonSerializer.Deserialize<Time>(timeSerialized);
// {
//    DateTime = 1/1/0001 12:00:00 AM
//    Hour = 1
//    Minute = 1
//    Second = 1
// }

This could be fixed a couple different ways:

  1. Change the DateTime property from { get; } to { get; init; }. This also fixes deserialization with Newtonsoft.Json. I can't think of any downside.
  2. Add [JsonConstructor] to one of the constructors so the JSON properties are used in the constructor arguments.
  3. Provide JsonConverters that mimic KiotaJsonSerializer. This would also fix serialization so they are written as strings instead of objects, but that might technically be a breaking change.</issue_description>

<agent_instructions>Please add JsonConstructor attribute to the best choice of constructor for these classes and write tests to validate the functionality.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@gavinbarron Thanks for raising this @n0spaces

init is a c#9 feature, as we produce code to be compatible with netstandard2.0 this restricts the language features that we can use. At present we do use a few C#8.0 features that can be used without additional types and remain compatible with netstandard2.0. So that's out at the moment.

the [JsonConstructor] option is probably the best way forward here as it represents the lowest risk.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…ation

Co-authored-by: gavinbarron <7122716+gavinbarron@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix deserialization issue for Date and Time types in JSON Add JsonConstructor attributes to Date and Time for System.Text.Json deserialization Feb 4, 2026
Copilot AI requested a review from gavinbarron February 4, 2026 00:38
@gavinbarron gavinbarron marked this pull request as ready for review February 6, 2026 03:07
@gavinbarron gavinbarron requested a review from a team as a code owner February 6, 2026 03:07
@gavinbarron
Copy link
Contributor

@copilot please run dotnet format

Co-authored-by: gavinbarron <7122716+gavinbarron@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 6, 2026

@copilot please run dotnet format

Done! Applied formatting in commit 09fab74.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 6, 2026

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

System.Text.Json.JsonSerializer cannot deserialize Date and Time types using default converters

2 participants