Repository navigation
Add support for Date/TimeOnly to STJ #51302
Copy link
Copy link
Closed
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationEarly API idea and discussion, it is NOT ready for implementationarea-System.Text.Json
Milestone
Description
Background and Motivation
The recently added types DateOnly and TimeOnly should be fully supported by System.Text.Json. Even when it's possible to make them serializable via a built-in or custom converter, adding support to Utf8JsonReader/Writer brings them on par with other types like DateTimeOffset.
Proposed API
namespace System.Text.Json
{
public ref partial struct Utf8JsonReader
{
+ public DateOnly GetDateOnly();
+ public TimeOnly GetTimeOnly();
+ public bool TryGetDateOnly(out DateOnly value);
+ public bool TryGetTimeOnly(out TimeOnly value);
}
public sealed partial class Utf8JsonWriter
{
+ public void WriteString(JsonEncodedText propertyName, DateOnly value);
+ public void WriteString(JsonEncodedText propertyName, TimeOnly value);
+ public void WriteString(string propertyName, DateOnly value);
+ public void WriteString(string propertyName, TimeOnly value);
+ public void WriteString(ReadOnlySpan<char> propertyName, DateOnly value);
+ public void WriteString(ReadOnlySpan<char> propertyName, TimeOnly value);
+ public void WriteString(ReadOnlySpan<byte> utf8PropertyName, DateOnly value);
+ public void WriteString(ReadOnlySpan<byte> utf8PropertyName, TimeOnly value);
+ public void WriteStringValue(DateOnly value);
+ public void WriteStringValue(TimeOnly value);
}
public readonly partial struct JsonElement
{
+ public DateOnly GetDateOnly();
+ public TimeOnly GetTimeOnly();
+ public bool TryGetDateOnly(out DateOnly value)
+ public bool TryGetTimeOnly(out TimeOnly value)
}
}
namespace System.Text.Json.Node
{
public abstract partial class JsonNode
{
+ public static explicit operator DateOnly(JsonNode value);
+ public static explicit operator TimeOnly(JsonNode value);
+ public static explicit operator DateOnly?(JsonNode? value);
+ public static explicit operator TimeOnly?(JsonNode? value);
+ public static implicit operator JsonNode(DateOnly value);
+ public static implicit operator JsonNode(TimeOnly value);
+ public static implicit operator JsonNode?(DateOnly? value);
+ public static implicit operator JsonNode?(TimeOnly? value);
}
}
namespace System.Text.Json.Serialization.Metadata
{
public static partial class JsonMetadataServices
{
+ public static JsonConverter<DateOnly> DateOnlyConverter { get; }
+ public static JsonConverter<TimeOnly> TimeOnlyConverter { get; }
}
}
namespace System.Buffers.Text
{
public static partial class Utf8Formatter
{
+ public static bool TryFormat(DateOnly value, Span<byte> destination, out int bytesWritten, StandardFormat format = default);
+ public static bool TryFormat(TimeOnly value, Span<byte> destination, out int bytesWritten, StandardFormat format = default);
}
public static partial class Utf8Parser
{
+ public static bool TryParse(ReadOnlySpan<byte> source, out DateOnly value, out int bytesConsumed, char standardFormat = default);
+ public static bool TryParse(ReadOnlySpan<byte> source, out TimeOnly value, out int bytesConsumed, char standardFormat = default);
}
}Usage Examples
var value = reader.GetDateOnly();
writer.WriteStringValue(value);Alternative Designs
As mentioned it would be possible to just add serialization converters for these types without supporting the Utf8Reader/Writer scenarios.
Risks
Obviously this API cannot be added to platforms prior .NET 6. Thus it would be necessary to only include it in the .NET 6 version of STJ.
Updates
- Added
Utf8Formatter.TryFormatandUtf8Parser.TryParseoverloads.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
api-suggestionEarly API idea and discussion, it is NOT ready for implementationEarly API idea and discussion, it is NOT ready for implementationarea-System.Text.Json
Activity
eiriktsarpalis commentedon Apr 15, 2021
Tagging @layomia and @steveharter for thoughts. It stands to reason that we should aim to release support for the new types in .NET 6 (presumably conforming to ISO 8601 date only/time only formats).
devsko commentedon Apr 15, 2021
A small collection of ISO 8601 related issues:
devsko commentedon Apr 16, 2021
IMO the formatting/parsing of the new types should be implemented identically as for DateTime(Offset) (and TimeSpan). Any changes wrt ISO 8601 are orthogonal and should be applied to all of these types in the same way.
mattjohnsonpint commentedon May 7, 2021
I agree these should have full parity with the other date/time types.
In the meantime, here's some simple converters that will do the trick. (Though ultimately, they should not be needed.)
ericstj commentedon May 25, 2021
cc @tarekgh - should we look for elsewhere in the framework that has special handling for DateTime/DateTimeOffset and ensure we add handling for DateOnly/TimeOnly as well?
tarekgh commentedon May 25, 2021
@ericstj yes this is in our radar but not really planned for 6.0 release. It would be good candidate for 7.0 release. If we get a chance before then we should consider it.
layomia commentedon Jun 1, 2021
We likely won't get to this level of support in .NET 6.0, but can support them in
JsonSerializernow - #53539.LaughingJohn commentedon Aug 2, 2021
Are XmlSerializer and DataContractSerializer also going to be supported?
eiriktsarpalis commentedon Aug 2, 2021
@LaughingJohn I created a separate issue on that question to be considered by the area owners of those serializers.
LaughingJohn commentedon Aug 2, 2021
Thanks @eiriktsarpalis!
eiriktsarpalis commentedon Oct 22, 2021
Closing in favor of #53539.