< Summary

Information
Line coverage
48%
Covered lines: 24
Uncovered lines: 25
Coverable lines: 49
Total lines: 101
Line coverage: 48.9%
Branch coverage
62%
Covered branches: 15
Total branches: 24
Branch coverage: 62.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Read(...)100%2283.33%
ReadAsPropertyNameCore(...)100%110%
ReadCore(...)59.09%222273.07%
Write(...)100%110%
WriteAsPropertyNameCore(...)100%110%
GetSchema(...)100%110%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\Converters\Value\TimeOnlyConverter.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Buffers.Text;
 5using System.Diagnostics;
 6using System.Text.Json.Nodes;
 7using System.Text.Json.Schema;
 8
 9namespace System.Text.Json.Serialization.Converters
 10{
 11    internal sealed class TimeOnlyConverter : JsonPrimitiveConverter<TimeOnly>
 12    {
 13        private const int MinimumTimeOnlyFormatLength = 3; // h:m
 14        private const int MaximumTimeOnlyFormatLength = 16; // hh:mm:ss.fffffff
 15        private const int MaximumEscapedTimeOnlyFormatLength = JsonConstants.MaxExpansionFactorWhileEscaping * MaximumTi
 16
 17        public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 63418        {
 63419            if (reader.TokenType != JsonTokenType.String)
 35620            {
 35621                ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType);
 22            }
 23
 27824            return ReadCore(ref reader);
 025        }
 26
 27        internal override TimeOnly ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerO
 028        {
 029            Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
 030            return ReadCore(ref reader);
 031        }
 32
 33        private static TimeOnly ReadCore(ref Utf8JsonReader reader)
 27834        {
 27835            Debug.Assert(reader.TokenType is JsonTokenType.String or JsonTokenType.PropertyName);
 36
 27837            if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, MinimumTimeOnlyFormatLength, MaximumEscapedTimeOnlyF
 19438            {
 19439                ThrowHelper.ThrowFormatException(DataType.TimeOnly);
 40            }
 41
 42            scoped ReadOnlySpan<byte> source;
 8443            if (!reader.HasValueSequence && !reader.ValueIsEscaped)
 5444            {
 5445                source = reader.ValueSpan;
 5446            }
 47            else
 3048            {
 3049                Span<byte> stackSpan = stackalloc byte[MaximumEscapedTimeOnlyFormatLength];
 3050                int bytesWritten = reader.CopyString(stackSpan);
 851                source = stackSpan.Slice(0, bytesWritten);
 852            }
 53
 6254            byte firstChar = source[0];
 6255            int firstSeparator = source.IndexOfAny((byte)'.', (byte)':');
 6256            if (!JsonHelpers.IsDigit(firstChar) || firstSeparator < 0 || source[firstSeparator] == (byte)'.')
 6257            {
 58                // Note: Utf8Parser.TryParse permits leading whitespace, negative values
 59                // and numbers of days so we need to exclude these cases here.
 6260                ThrowHelper.ThrowFormatException(DataType.TimeOnly);
 61            }
 62
 063            bool result = Utf8Parser.TryParse(source, out TimeSpan timespan, out int bytesConsumed, 'c');
 64
 65            // Note: Utf8Parser.TryParse will return true for invalid input so
 66            // long as it starts with an integer. Example: "2021-06-18" or
 67            // "1$$$$$$$$$$". We need to check bytesConsumed to know if the
 68            // entire source was actually valid.
 69
 070            if (!result || source.Length != bytesConsumed)
 071            {
 072                ThrowHelper.ThrowFormatException(DataType.TimeOnly);
 73            }
 74
 075            Debug.Assert(TimeOnly.MinValue.ToTimeSpan() <= timespan && timespan <= TimeOnly.MaxValue.ToTimeSpan());
 076            return TimeOnly.FromTimeSpan(timespan);
 077        }
 78
 79        public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options)
 080        {
 081            Span<byte> output = stackalloc byte[MaximumTimeOnlyFormatLength];
 82
 083            bool result = Utf8Formatter.TryFormat(value.ToTimeSpan(), output, out int bytesWritten, 'c');
 084            Debug.Assert(result);
 85
 086            writer.WriteStringValue(output.Slice(0, bytesWritten));
 087        }
 88
 89        internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions opti
 090        {
 091            Span<byte> output = stackalloc byte[MaximumTimeOnlyFormatLength];
 92
 093            bool result = Utf8Formatter.TryFormat(value.ToTimeSpan(), output, out int bytesWritten, 'c');
 094            Debug.Assert(result);
 95
 096            writer.WritePropertyName(output.Slice(0, bytesWritten));
 097        }
 98
 099        internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String, Format = 
 100    }
 101}