-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
base: main
Are you sure you want to change the base?
Conversation
…izer - Core implementation Co-authored-by: StephenMolloy <[email protected]>
Co-authored-by: StephenMolloy <[email protected]>
…ethods and test types Co-authored-by: StephenMolloy <[email protected]>
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
This PR implements comprehensive support for
DateOnly
andTimeOnly
types in bothXmlSerializer
andDataContractSerializer
, addressing the serialization gap for these .NET 6+ date/time types.Changes Made
Core Serialization Support
ToString(DateOnly)
,ToDateOnly(string)
,ToString(TimeOnly)
, andToTimeOnly(string)
methodsWrite_DateOnly/Read_DateOnly
andWrite_TimeOnly/Read_TimeOnly
methodsDateOnlyDataContract
andTimeOnlyDataContract
classes with full serialization supportXML Format
DateOnly
serializes using ISO 8601 date format (YYYY-MM-DD)TimeOnly
serializes using precise time format (HH:mm:ss.fffffff)Implementation Details
Test Coverage
Added comprehensive tests covering:
Example Usage
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.