Skip to content

Commit

Permalink
TimeOnlyConverter should tolerate more formats in line with TimeSpanC…
Browse files Browse the repository at this point in the history
…onverter (#106053)

* Allow values that would also have been parsed correctly using TimeOnly.Parse(string, CultureInfo.InvariantCulture)

* Fix comment and add test

* Add test cases for single-digit hour and minute values
  • Loading branch information
etemi committed Aug 8, 2024
1 parent 7c0364e commit 8a5f842
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
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

0 comments on commit 8a5f842

Please sign in to comment.