Skip to content

Support DateOnly and TimeOnly in JsonSerializer #53539

@layomia

Description

@layomia

#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.

Metadata

Metadata

Labels

Cost:SWork that requires one engineer up to 1 weekPriority:2Work that is important, but not critical for the releaseTeam:LibrariesUser StoryA single user-facing feature. Can be grouped under an epic.api-approvedAPI was approved in API review, it can be implementedarea-System.Text.JsonblockingMarks issues that we want to fast track in order to unblock other important work

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions