< Summary

Information
Line coverage
50%
Covered lines: 22
Uncovered lines: 22
Coverable lines: 44
Total lines: 83
Line coverage: 50%
Branch coverage
62%
Covered branches: 10
Total branches: 16
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(...)80%101080.95%
Write(...)0%220%
WriteAsPropertyNameCore(...)0%220%
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\DateOnlyConverter.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.Diagnostics;
 5using System.Globalization;
 6using System.Text.Json.Schema;
 7
 8namespace System.Text.Json.Serialization.Converters
 9{
 10    internal sealed class DateOnlyConverter : JsonPrimitiveConverter<DateOnly>
 11    {
 12        public const int FormatLength = 10; // YYYY-MM-DD
 13        public const int MaxEscapedFormatLength = FormatLength * JsonConstants.MaxExpansionFactorWhileEscaping;
 14
 15        public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 63416        {
 63417            if (reader.TokenType != JsonTokenType.String)
 35618            {
 35619                ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType);
 20            }
 21
 27822            return ReadCore(ref reader);
 023        }
 24
 25        internal override DateOnly ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerO
 026        {
 027            Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
 028            return ReadCore(ref reader);
 029        }
 30
 31        private static DateOnly ReadCore(ref Utf8JsonReader reader)
 27832        {
 27833            if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, FormatLength, MaxEscapedFormatLength))
 26034            {
 26035                ThrowHelper.ThrowFormatException(DataType.DateOnly);
 36            }
 37
 38            scoped ReadOnlySpan<byte> source;
 1839            if (!reader.HasValueSequence && !reader.ValueIsEscaped)
 1140            {
 1141                source = reader.ValueSpan;
 1142            }
 43            else
 744            {
 745                Span<byte> stackSpan = stackalloc byte[MaxEscapedFormatLength];
 746                int bytesWritten = reader.CopyString(stackSpan);
 47
 48                // CopyString can unescape which can change the length, so we need to perform the length check again.
 149                if (bytesWritten < FormatLength)
 050                {
 051                    ThrowHelper.ThrowFormatException(DataType.DateOnly);
 52                }
 53
 154                source = stackSpan.Slice(0, bytesWritten);
 155            }
 56
 1257            if (!JsonHelpers.TryParseAsIso(source, out DateOnly value))
 1258            {
 1259                ThrowHelper.ThrowFormatException(DataType.DateOnly);
 60            }
 61
 062            return value;
 063        }
 64
 65        public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
 066        {
 067            Span<byte> buffer = stackalloc byte[FormatLength];
 068            bool formattedSuccessfully = value.TryFormat(buffer, out int charsWritten, "O", CultureInfo.InvariantCulture
 069            Debug.Assert(formattedSuccessfully && charsWritten == FormatLength);
 070            writer.WriteStringValue(buffer);
 071        }
 72
 73        internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions opti
 074        {
 075            Span<byte> buffer = stackalloc byte[FormatLength];
 076            bool formattedSuccessfully = value.TryFormat(buffer, out int charsWritten, "O", CultureInfo.InvariantCulture
 077            Debug.Assert(formattedSuccessfully && charsWritten == FormatLength);
 078            writer.WritePropertyName(buffer);
 079        }
 80
 081        internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String, Format = 
 82    }
 83}