diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs index 75b177ecb28391..2eb9b16c86b20b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs @@ -666,28 +666,7 @@ internal bool TryGetValue(int index, out DateTime value) ReadOnlySpan data = _utf8Json.Span; ReadOnlySpan segment = data.Slice(row.Location, row.SizeOrLength); - if (!JsonHelpers.IsValidDateTimeOffsetParseLength(segment.Length)) - { - value = default; - return false; - } - - // Segment needs to be unescaped - if (row.HasComplexChildren) - { - return JsonReaderHelper.TryGetEscapedDateTime(segment, out value); - } - - Debug.Assert(segment.IndexOf(JsonConstants.BackSlash) == -1); - - if (JsonHelpers.TryParseAsISO(segment, out DateTime tmp)) - { - value = tmp; - return true; - } - - value = default; - return false; + return JsonReaderHelper.TryGetValue(segment, row.HasComplexChildren, out value); } internal bool TryGetValue(int index, out DateTimeOffset value) @@ -701,28 +680,7 @@ internal bool TryGetValue(int index, out DateTimeOffset value) ReadOnlySpan data = _utf8Json.Span; ReadOnlySpan segment = data.Slice(row.Location, row.SizeOrLength); - if (!JsonHelpers.IsValidDateTimeOffsetParseLength(segment.Length)) - { - value = default; - return false; - } - - // Segment needs to be unescaped - if (row.HasComplexChildren) - { - return JsonReaderHelper.TryGetEscapedDateTimeOffset(segment, out value); - } - - Debug.Assert(segment.IndexOf(JsonConstants.BackSlash) == -1); - - if (JsonHelpers.TryParseAsISO(segment, out DateTimeOffset tmp)) - { - value = tmp; - return true; - } - - value = default; - return false; + return JsonReaderHelper.TryGetValue(segment, row.HasComplexChildren, out value); } internal bool TryGetValue(int index, out Guid value) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs index b426f61cbb31dd..3c748e8fbb988b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs @@ -79,6 +79,32 @@ public static bool IsTokenTypePrimitive(JsonTokenType tokenType) => // Otherwise, return false. public static bool IsHexDigit(byte nextByte) => HexConverter.IsHexChar(nextByte); + public static bool TryGetValue(ReadOnlySpan segment, bool isEscaped, out DateTime value) + { + if (!JsonHelpers.IsValidDateTimeOffsetParseLength(segment.Length)) + { + value = default; + return false; + } + + // Segment needs to be unescaped + if (isEscaped) + { + return TryGetEscapedDateTime(segment, out value); + } + + Debug.Assert(segment.IndexOf(JsonConstants.BackSlash) == -1); + + if (JsonHelpers.TryParseAsISO(segment, out DateTime tmp)) + { + value = tmp; + return true; + } + + value = default; + return false; + } + public static bool TryGetEscapedDateTime(ReadOnlySpan source, out DateTime value) { Debug.Assert(source.Length <= JsonConstants.MaximumEscapedDateTimeOffsetParseLength); @@ -101,6 +127,32 @@ public static bool TryGetEscapedDateTime(ReadOnlySpan source, out DateTime return false; } + public static bool TryGetValue(ReadOnlySpan segment, bool isEscaped, out DateTimeOffset value) + { + if (!JsonHelpers.IsValidDateTimeOffsetParseLength(segment.Length)) + { + value = default; + return false; + } + + // Segment needs to be unescaped + if (isEscaped) + { + return TryGetEscapedDateTimeOffset(segment, out value); + } + + Debug.Assert(segment.IndexOf(JsonConstants.BackSlash) == -1); + + if (JsonHelpers.TryParseAsISO(segment, out DateTimeOffset tmp)) + { + value = tmp; + return true; + } + + value = default; + return false; + } + public static bool TryGetEscapedDateTimeOffset(ReadOnlySpan source, out DateTimeOffset value) { Debug.Assert(source.Length <= JsonConstants.MaximumEscapedDateTimeOffsetParseLength); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs index 7b58a24a9c4d79..5d59810e106b4d 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.TryGet.cs @@ -1260,30 +1260,10 @@ internal bool TryGetDateTimeCore(out DateTime value) } else { - if (!JsonHelpers.IsInRangeInclusive(ValueSpan.Length, JsonConstants.MinimumDateTimeParseLength, JsonConstants.MaximumEscapedDateTimeOffsetParseLength)) - { - value = default; - return false; - } - span = ValueSpan; } - if (ValueIsEscaped) - { - return JsonReaderHelper.TryGetEscapedDateTime(span, out value); - } - - Debug.Assert(span.IndexOf(JsonConstants.BackSlash) == -1); - - if (JsonHelpers.TryParseAsISO(span, out DateTime tmp)) - { - value = tmp; - return true; - } - - value = default; - return false; + return JsonReaderHelper.TryGetValue(span, ValueIsEscaped, out value); } /// @@ -1325,30 +1305,10 @@ internal bool TryGetDateTimeOffsetCore(out DateTimeOffset value) } else { - if (!JsonHelpers.IsInRangeInclusive(ValueSpan.Length, JsonConstants.MinimumDateTimeParseLength, JsonConstants.MaximumEscapedDateTimeOffsetParseLength)) - { - value = default; - return false; - } - span = ValueSpan; } - if (ValueIsEscaped) - { - return JsonReaderHelper.TryGetEscapedDateTimeOffset(span, out value); - } - - Debug.Assert(span.IndexOf(JsonConstants.BackSlash) == -1); - - if (JsonHelpers.TryParseAsISO(span, out DateTimeOffset tmp)) - { - value = tmp; - return true; - } - - value = default; - return false; + return JsonReaderHelper.TryGetValue(span, ValueIsEscaped, out value); } ///