-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
#51302 proposes adding DateOnly
and TimeOnly
support to System.Text.Json: on the serializer, reader, writer, and DOM types. We won't get to all of this in 6.0 but can support them at the JsonSerializer
level now, using internal converters and building on ISO 8601 support for DateTime
and DateTimeOffset
.
API Proposal
We don't need new API to support these types in the existing (non source-gen) serializer programming model. The implementation will be based on new, internal JsonConverter<T>
s. Support will be automatic since the serializer initializes converters for all types during its warm-up phase.
// No new API or opt-in needed
JsonSerializer.Deserialize<DateOnly>(json);
JsonSerializer.Serialize(new MyClassWithTimeOnlyProp());
When the JSON source generation feature (#45448) is used, we'll need API like the following so that the generator can generate code that initializes those converters as-needed in a user's project.
namespace System.Text.Json.Serialization.Metadata
{
public static partial class JsonMetadataServices
{
public static JsonConverter<DateOnly> DateOnlyConverter { get; }
public static JsonConverter<TimeOnly> TimeOnlyConverter { get; }
}
}
Format
DateOnly
and TimeOnly
will be (de)serialized using the following formats:
Format | Description | |
---|---|---|
DateOnly | "yyyy'-'MM'-'dd" |
ISO 8601: Calendar date |
TimeOnly | "HH':'mm':'ss[FFFFFFF]" |
ISO 8601: Time without UTC offset information |
See Date and time components for more information.
EDIT @eiriktsarpalis should be reviewed in conjunction with #29932
FYI @devsko, @mattjohnsonpint, @eiriktsarpalis, @steveharter, @tarekgh, @ericstj.