Skip to content

TimeOnlyConverter should tolerate more formats in line with TimeSpanConverter #106053

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

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace System.Text.Json.Serialization.Converters
{
internal sealed class TimeOnlyConverter : JsonPrimitiveConverter<TimeOnly>
{
private const int MinimumTimeOnlyFormatLength = 8; // hh:mm:ss
private const int MinimumTimeOnlyFormatLength = 3; // h:m
private const int MaximumTimeOnlyFormatLength = 16; // hh:mm:ss.fffffff
private const int MaximumEscapedTimeOnlyFormatLength = JsonConstants.MaxExpansionFactorWhileEscaping * MaximumTimeOnlyFormatLength;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,11 @@ private static void DeserializeLongJsonString(int stringLength)
}

[Theory]
[InlineData("1:2")]
[InlineData("01:2")]
[InlineData("1:02")]
[InlineData("01:23:1")]
[InlineData("1.1:1:1.0")]
[InlineData("1:00:00")]
[InlineData("1")]
[InlineData("10")]
Expand Down Expand Up @@ -654,6 +659,12 @@ public static void DateOnly_Read_Failure(string json, bool addQuotes = true)
}

[Theory]
[InlineData("1:2", "01:02")]
[InlineData("01:2", "01:02")]
[InlineData("01:23:1", "01:23:01")]
[InlineData("1:00:00")] // 'g' Format
[InlineData("00:00")]
[InlineData("23:59")]
[InlineData("23:59:59")]
[InlineData("23:59:59.9", "23:59:59.9000000")]
[InlineData("02:48:05.4775807")]
Expand All @@ -680,8 +691,9 @@ public static void TimeOnly_Read_Nullable_Tests()
}

[Theory]
[InlineData("00:00")]
[InlineData("23:59")]
[InlineData("0")]
[InlineData("01")]
[InlineData("01:")]
[InlineData("\t23:59:59")] // Otherwise valid but has invalid json character
[InlineData("\\t23:59:59")] // Otherwise valid but has leading whitespace
[InlineData("23:59:59 ")] // Otherwise valid but has trailing whitespace
Expand All @@ -693,7 +705,6 @@ public static void TimeOnly_Read_Nullable_Tests()
[InlineData("900000000.00:00:00")]
[InlineData("1.00:00:00")]
[InlineData("0.00:00:00")]
[InlineData("1:00:00")] // 'g' Format
[InlineData("1:2:00:00")] // 'g' Format
[InlineData("+00:00:00")]
[InlineData("2021-06-18")]
Expand Down
Loading