-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DateOnly and TimeOnly literal converters (#282)
Motivation ---------- We'd like to add support for DateOnly and TimeOnly when targeting modern .NET and using System.Text.Json. When we do, we'll need support for header and query string literals as well. Modifications ------------- - Add DateOnlyLiteralConverter and TimeOnlyLiteralConverter, mirroring the serialization formats used by System.Text.Json - Register on LiteralConverterRegistry Results ------- No changes for now unless an extension is used to change schema generation to use DateOnly and TimeOnly.
- Loading branch information
1 parent
8380f19
commit 7c418a4
Showing
4 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/Yardarm.Client/Serialization/Literals/Converters/DateOnlyLiteralConverter.net6.0.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
|
||
namespace RootNamespace.Serialization.Literals.Converters; | ||
|
||
internal sealed class DateOnlyLiteralConverter : ValueTypeLiteralConverter<DateOnly> | ||
{ | ||
protected override DateOnly ReadCore(string value, string? format) => | ||
DateOnly.ParseExact(value, "o"); | ||
|
||
public override string Write(DateOnly value, string? format) => | ||
value.ToString("o"); | ||
|
||
public override bool TryWrite(DateOnly value, ReadOnlySpan<char> format, Span<char> destination, out int charsWritten) => | ||
value.TryFormat(destination, out charsWritten, "o"); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/Yardarm.Client/Serialization/Literals/Converters/TimeOnlyLiteralConverter.net6.0.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using Yardarm.Client.Internal; | ||
|
||
namespace RootNamespace.Serialization.Literals.Converters; | ||
|
||
internal sealed class TimeOnlyLiteralConverter : ValueTypeLiteralConverter<TimeOnly> | ||
{ | ||
protected override TimeOnly ReadCore(string value, string? format) | ||
{ | ||
ThrowHelper.ThrowIfNull(value); | ||
|
||
char firstChar = value[0]; | ||
int firstSeparator = value.AsSpan().IndexOfAny('.', ':'); | ||
if (!char.IsDigit(firstChar) || firstSeparator < 0 || value[firstSeparator] == '.') | ||
{ | ||
// Note: TimeSpan.ParseExact permits leading whitespace, negative values | ||
// and numbers of days so we need to exclude these cases here. | ||
ThrowHelper.ThrowFormatException("The value is not in a supported TimeOnly format."); | ||
} | ||
|
||
return TimeOnly.FromTimeSpan(TimeSpan.ParseExact(value, "c", null)); | ||
} | ||
|
||
public override string Write(TimeOnly value, string? format) => | ||
value.ToTimeSpan().ToString("c"); | ||
|
||
public override bool TryWrite(TimeOnly value, ReadOnlySpan<char> format, Span<char> destination, out int charsWritten) => | ||
value.ToTimeSpan().TryFormat(destination, out charsWritten, "c"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters