< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 50
Coverable lines: 50
Total lines: 104
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 22
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Read(...)0%220%
ReadAsPropertyNameCore(...)100%110%
ReadCore(...)0%20200%
Write(...)100%110%
WriteAsPropertyNameCore(...)100%110%
GetSchema(...)100%110%
GetSupportedJsonValueTypes(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/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)
 018        {
 019            if (reader.TokenType != JsonTokenType.String)
 020            {
 021                ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType);
 22            }
 23
 024            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 unsafe TimeOnly ReadCore(ref Utf8JsonReader reader)
 034        {
 035            Debug.Assert(reader.TokenType is JsonTokenType.String or JsonTokenType.PropertyName);
 36
 037            if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, MinimumTimeOnlyFormatLength, MaximumEscapedTimeOnlyF
 038            {
 039                ThrowHelper.ThrowFormatException(DataType.TimeOnly);
 40            }
 41
 42            scoped ReadOnlySpan<byte> source;
 043            if (!reader.HasValueSequence && !reader.ValueIsEscaped)
 044            {
 045                source = reader.ValueSpan;
 046            }
 47            else
 048            {
 049                Span<byte> stackSpan = stackalloc byte[MaximumEscapedTimeOnlyFormatLength];
 050                int bytesWritten = reader.CopyString(stackSpan);
 051                source = stackSpan.Slice(0, bytesWritten);
 052            }
 53
 054            byte firstChar = source[0];
 055            int firstSeparator = source.IndexOfAny((byte)'.', (byte)':');
 056            if (!JsonHelpers.IsDigit(firstChar) || firstSeparator < 0 || source[firstSeparator] == (byte)'.')
 057            {
 58                // Note: Utf8Parser.TryParse permits leading whitespace, negative values
 59                // and numbers of days so we need to exclude these cases here.
 060                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 unsafe 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 unsafe void WriteAsPropertyNameCore(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptio
 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
 0101        internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.String;
 102    }
 103}
 104