< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 45
Coverable lines: 45
Total lines: 86
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
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%10100%
Write(...)0%220%
WriteAsPropertyNameCore(...)0%220%
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/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)
 016        {
 017            if (reader.TokenType != JsonTokenType.String)
 018            {
 019                ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType);
 20            }
 21
 022            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 unsafe DateOnly ReadCore(ref Utf8JsonReader reader)
 032        {
 033            if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, FormatLength, MaxEscapedFormatLength))
 034            {
 035                ThrowHelper.ThrowFormatException(DataType.DateOnly);
 36            }
 37
 38            scoped ReadOnlySpan<byte> source;
 039            if (!reader.HasValueSequence && !reader.ValueIsEscaped)
 040            {
 041                source = reader.ValueSpan;
 042            }
 43            else
 044            {
 045                Span<byte> stackSpan = stackalloc byte[MaxEscapedFormatLength];
 046                int bytesWritten = reader.CopyString(stackSpan);
 47
 48                // CopyString can unescape which can change the length, so we need to perform the length check again.
 049                if (bytesWritten < FormatLength)
 050                {
 051                    ThrowHelper.ThrowFormatException(DataType.DateOnly);
 52                }
 53
 054                source = stackSpan.Slice(0, bytesWritten);
 055            }
 56
 057            if (!JsonHelpers.TryParseAsIso(source, out DateOnly value))
 058            {
 059                ThrowHelper.ThrowFormatException(DataType.DateOnly);
 60            }
 61
 062            return value;
 063        }
 64
 65        public override unsafe 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 unsafe void WriteAsPropertyNameCore(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptio
 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
 083        internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.String;
 84    }
 85}
 86