Skip to content

Add DateOnly/TimeOnly support to XmlSerializer and DataContractSerializer #118549

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 8, 2025

This PR implements comprehensive support for DateOnly and TimeOnly types in both XmlSerializer and DataContractSerializer, addressing the serialization gap for these .NET 6+ date/time types.

Changes Made

Core Serialization Support

  • XmlConvert: Added public ToString(DateOnly), ToDateOnly(string), ToString(TimeOnly), and ToTimeOnly(string) methods
  • XmlConverter: Added internal wrapper methods for DataContractSerializer error handling
  • XmlSerializer: Added primitive serializers with Write_DateOnly/Read_DateOnly and Write_TimeOnly/Read_TimeOnly methods
  • DataContractSerializer: Added DateOnlyDataContract and TimeOnlyDataContract classes with full serialization support

XML Format

  • DateOnly serializes using ISO 8601 date format (YYYY-MM-DD)
  • TimeOnly serializes using precise time format (HH:mm:ss.fffffff)
  • Both formats are compatible with existing XML schema standards and provide full round-trip fidelity

Implementation Details

  • Updated type registration systems in both serializers
  • Added XML dictionary entries for DataContractSerializer namespace handling
  • Extended writer/reader delegators with appropriate conversion methods
  • Updated reference assemblies to expose new public APIs

Test Coverage

Added comprehensive tests covering:

  • Round-trip serialization for both primitive types and object properties
  • Edge cases (min/max values, leap years, precision handling)
  • Error handling for invalid input formats
  • Default value scenarios
  • Both XmlSerializer and DataContractSerializer workflows

Example Usage

// XmlSerializer
var data = new MyClass { Date = new DateOnly(2024, 1, 15), Time = new TimeOnly(14, 30, 45) };
var serializer = new XmlSerializer(typeof(MyClass));
// Produces: <Date>2024-01-15</Date><Time>14:30:45.0000000</Time>

// DataContractSerializer  
[DataContract]
public class MyData 
{
    [DataMember] public DateOnly Date { get; set; }
    [DataMember] public TimeOnly Time { get; set; }
}
var dcs = new DataContractSerializer(typeof(MyData));
// Produces: <DateOnly>2024-01-15</DateOnly><TimeOnly>14:30:45.0000000</TimeOnly>

This implementation maintains backward compatibility and follows existing serialization patterns for other primitive types.

Fixes #56711.


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

@Copilot Copilot AI changed the title [WIP] Add DateIOnly/TimeOnly support to XmlSerializer and DataContractSerializer Add DateOnly/TimeOnly support to XmlSerializer and DataContractSerializer Aug 8, 2025
@Copilot Copilot AI requested a review from StephenMolloy August 8, 2025 22:54
Copilot finished work on behalf of StephenMolloy August 8, 2025 22:54
Comment on lines 34 to 41
internal static Type TypeOfVoid => field ??= typeof(void);
internal static Type TypeOfByteArray => field ??= typeof(byte[]);
internal static Type TypeOfTimeSpan => field ??= typeof(TimeSpan);
internal static Type TypeOfDateOnly => field ??= typeof(DateOnly);
internal static Type TypeOfTimeOnly => field ??= typeof(TimeOnly);
internal static Type TypeOfGuid => field ??= typeof(Guid);
internal static Type TypeOfDateTimeOffset => field ??= typeof(DateTimeOffset);
internal static Type TypeOfDateTimeOffsetAdapter => field ??= typeof(DateTimeOffsetAdapter);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's highly doubtful that caching the result of typeof() is helpful; we can remove it either now or in a separate PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, caching typeof like this is a de-optimization in current .NET. It would be best to delete these fields and use typeof directly in all places they are used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add DateIOnly/TimeOnly support to XmlSerializer and DataContractSerializer
4 participants