-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add support for Date/TimeOnly to STJ #51302
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsBackground and MotivationThe recently added types Proposed APInamespace System.Text.Json
{
public ref partial struct Utf8JsonReader
{
+ public System.DateOnly GetDateOnly();
+ public System.TimeOnly GetTimeOnly();
+ public bool TryGetDateOnly(out System.DateOnly value);
+ public bool TryGetTimeOnly(out System.TimeOnly value);
}
public sealed partial class Utf8JsonWriter
{
+ public void WriteString(JsonEncodedText propertyName, System.DateOnly value);
+ public void WriteString(JsonEncodedText propertyName, System.TimeOnly value);
+ public void WriteString(string propertyName, System.DateOnly value);
+ public void WriteString(string propertyName, System.TimeOnly value);
+ public void WriteString(ReadOnlySpan<char> propertyName, System.DateOnly value);
+ public void WriteString(ReadOnlySpan<char> propertyName, System.TimeOnly value);
+ public void WriteString(ReadOnlySpan<byte> utf8PropertyName, System.DateOnly value);
+ public void WriteString(ReadOnlySpan<byte> utf8PropertyName, System.TimeOnly value);
+ public void WriteStringValue(System.DateOnly value);
+ public void WriteStringValue(System.TimeOnly value);
}
public readonly partial struct JsonElement
{
+ public System.DateOnly GetDateOnly();
+ public System.TimeOnly GetTimeOnly();
+ public bool TryGetDateOnly(out System.DateOnly value)
+ public bool TryGetTimeOnly(out System.TimeOnly value)
}
}
namespace System.Text.Json.Node
{
public abstract partial class JsonNode
{
+ public static explicit operator System.DateOnly(JsonNode value);
+ public static explicit operator System.TimeOnly(JsonNode value);
+ public static explicit operator System.DateOnly?(JsonNode? value);
+ public static explicit operator System.TimeOnly?(JsonNode? value);
+ public static implicit operator JsonNode(System.DateOnly value);
+ public static implicit operator JsonNode(System.TimeOnly value);
+ public static implicit operator JsonNode?(System.DateOnly? value);
+ public static implicit operator JsonNode?(System.TimeOnly? value);
}
}
namespace System.Text.Json.Serialization.Metadata
{
public static partial class JsonMetadataServices
{
+ public static JsonConverter<System.DateOnly> DateOnlyConverter { get; }
+ public static JsonConverter<System.TimeOnly> TimeOnlyConverter { get; }
}
} Usage Examples var value = reader.GetDateOnly();
writer.WriteStringValue(value); Alternative DesignsAs mentioned it would be possible to just add serialization converters for these types without supporting the RisksObviously 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.
|
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). |
A small collection of ISO 8601 related issues:
|
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. |
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.) public class DateOnlyJsonConverter : JsonConverter<DateOnly>
{
private const string DateFormat = "yyyy-MM-dd";
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateOnly.ParseExact(reader.GetString(), DateFormat, CultureInfo.InvariantCulture);
}
public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString(DateFormat, CultureInfo.InvariantCulture));
}
}
public class TimeOnlyJsonConverter : JsonConverter<TimeOnly>
{
private const string TimeFormat = "HH:mm:ss.FFFFFFF";
public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return TimeOnly.ParseExact(reader.GetString(), TimeFormat, CultureInfo.InvariantCulture);
}
public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString(TimeFormat, CultureInfo.InvariantCulture));
}
} |
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? |
@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. |
We likely won't get to this level of support in .NET 6.0, but can support them in |
Are XmlSerializer and DataContractSerializer also going to be supported? |
@LaughingJohn I created a separate issue on that question to be considered by the area owners of those serializers. |
Thanks @eiriktsarpalis! |
Closing in favor of #53539. |
Background and Motivation
The recently added types
DateOnly
andTimeOnly
should be fully supported bySystem.Text.Json
. Even when it's possible to make them serializable via a built-in or custom converter, adding support toUtf8JsonReader/Writer
brings them on par with other types likeDateTimeOffset
.Proposed API
Usage Examples
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
Utf8Formatter.TryFormat
andUtf8Parser.TryParse
overloads.The text was updated successfully, but these errors were encountered: