< Summary

Information
Line coverage
57%
Covered lines: 30
Uncovered lines: 22
Coverable lines: 52
Total lines: 104
Line coverage: 57.6%
Branch coverage
90%
Covered branches: 18
Total branches: 20
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Read(...)100%22100%
ReadAsPropertyNameCore(...)100%110%
ReadCore(...)88.88%1818100%
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\TimeSpanConverter.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 TimeSpanConverter : JsonPrimitiveConverter<TimeSpan>
 12    {
 13        private const int MinimumTimeSpanFormatLength = 1; // d
 14        private const int MaximumTimeSpanFormatLength = 26; // -dddddddd.hh:mm:ss.fffffff
 15        private const int MaximumEscapedTimeSpanFormatLength = JsonConstants.MaxExpansionFactorWhileEscaping * MaximumTi
 16
 17        public override TimeSpan 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);
 2225        }
 26
 27        internal override TimeSpan 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 TimeSpan ReadCore(ref Utf8JsonReader reader)
 27834        {
 27835            Debug.Assert(reader.TokenType is JsonTokenType.String or JsonTokenType.PropertyName);
 36
 27837            if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, MinimumTimeSpanFormatLength, MaximumEscapedTimeSpanF
 12038            {
 12039                ThrowHelper.ThrowFormatException(DataType.TimeSpan);
 40            }
 41
 42            scoped ReadOnlySpan<byte> source;
 15843            if (!reader.HasValueSequence && !reader.ValueIsEscaped)
 10644            {
 10645                source = reader.ValueSpan;
 10646            }
 47            else
 5248            {
 5249                Span<byte> stackSpan = stackalloc byte[MaximumEscapedTimeSpanFormatLength];
 5250                int bytesWritten = reader.CopyString(stackSpan);
 1251                source = stackSpan.Slice(0, bytesWritten);
 1252            }
 53
 11854            byte firstChar = source[0];
 11855            if (!JsonHelpers.IsDigit(firstChar) && firstChar != '-')
 7256            {
 57                // Note: Utf8Parser.TryParse allows for leading whitespace so we
 58                // need to exclude that case here.
 7259                ThrowHelper.ThrowFormatException(DataType.TimeSpan);
 60            }
 61
 4662            bool result = Utf8Parser.TryParse(source, out TimeSpan tmpValue, out int bytesConsumed, 'c');
 63
 64            // Note: Utf8Parser.TryParse will return true for invalid input so
 65            // long as it starts with an integer. Example: "2021-06-18" or
 66            // "1$$$$$$$$$$". We need to check bytesConsumed to know if the
 67            // entire source was actually valid.
 68
 4669            if (!result || source.Length != bytesConsumed)
 2470            {
 2471                ThrowHelper.ThrowFormatException(DataType.TimeSpan);
 72            }
 73
 2274            return tmpValue;
 2275        }
 76
 77        public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)
 078        {
 079            Span<byte> output = stackalloc byte[MaximumTimeSpanFormatLength];
 80
 081            bool result = Utf8Formatter.TryFormat(value, output, out int bytesWritten, 'c');
 082            Debug.Assert(result);
 83
 084            writer.WriteStringValue(output.Slice(0, bytesWritten));
 085        }
 86
 87        internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions opti
 088        {
 089            Span<byte> output = stackalloc byte[MaximumTimeSpanFormatLength];
 90
 091            bool result = Utf8Formatter.TryFormat(value, output, out int bytesWritten, 'c');
 092            Debug.Assert(result);
 93
 094            writer.WritePropertyName(output.Slice(0, bytesWritten));
 095        }
 96
 097        internal override JsonSchema? GetSchema(JsonNumberHandling _) => new()
 098        {
 099            Type = JsonSchemaType.String,
 0100            Comment = "Represents a System.TimeSpan value.",
 0101            Pattern = @"^-?(\d+\.)?\d{2}:\d{2}:\d{2}(\.\d{1,7})?$"
 0102        };
 103    }
 104}