< Summary

Information
Line coverage
37%
Covered lines: 19
Uncovered lines: 32
Coverable lines: 51
Total lines: 126
Line coverage: 37.2%
Branch coverage
59%
Covered branches: 13
Total branches: 22
Branch coverage: 59%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Read(...)75%4466.66%
ReadCore(...)83.33%121281.25%
Write(...)0%440%
ReadAsPropertyNameCore(...)100%110%
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\VersionConverter.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.Text.Json.Nodes;
 6using System.Text.Json.Schema;
 7
 8namespace System.Text.Json.Serialization.Converters
 9{
 10    internal sealed class VersionConverter : JsonPrimitiveConverter<Version?>
 11    {
 12#if NET
 13        private const int MinimumVersionLength = 3; // 0.0
 14
 15        private const int MaximumVersionLength = 43; // 2147483647.2147483647.2147483647.2147483647
 16
 17        private const int MaximumEscapedVersionLength = JsonConstants.MaxExpansionFactorWhileEscaping * MaximumVersionLe
 18#endif
 19
 20        public override Version? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 63421        {
 63422            if (reader.TokenType is JsonTokenType.Null)
 023            {
 024                return null;
 25            }
 26
 63427            if (reader.TokenType != JsonTokenType.String)
 35628            {
 35629                ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType);
 30            }
 31
 27832            return ReadCore(ref reader);
 033        }
 34
 35        private static Version ReadCore(ref Utf8JsonReader reader)
 27836        {
 27837            Debug.Assert(reader.TokenType is JsonTokenType.PropertyName or JsonTokenType.String);
 38
 39#if NET
 27840            if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, MinimumVersionLength, MaximumEscapedVersionLength))
 19441            {
 19442                ThrowHelper.ThrowFormatException(DataType.Version);
 43            }
 44
 8445            Span<char> charBuffer = stackalloc char[MaximumEscapedVersionLength];
 8446            int bytesWritten = reader.CopyString(charBuffer);
 2047            ReadOnlySpan<char> source = charBuffer.Slice(0, bytesWritten);
 48
 2049            if (!char.IsDigit(source[0]) || !char.IsDigit(source[^1]))
 1450            {
 51                // Since leading and trailing whitespaces are forbidden throughout System.Text.Json converters
 52                // we need to make sure that our input doesn't have them,
 53                // and if it has - we need to throw, to match behaviour of other converters
 54                // since Version.TryParse allows them and silently parses input to Version
 1455                ThrowHelper.ThrowFormatException(DataType.Version);
 56            }
 57
 658            if (Version.TryParse(source, out Version? result))
 059            {
 060                return result;
 61            }
 62#else
 63            string? versionString = reader.GetString();
 64            if (!string.IsNullOrEmpty(versionString) && (!char.IsDigit(versionString[0]) || !char.IsDigit(versionString[
 65            {
 66                // Since leading and trailing whitespaces are forbidden throughout System.Text.Json converters
 67                // we need to make sure that our input doesn't have them,
 68                // and if it has - we need to throw, to match behaviour of other converters
 69                // since Version.TryParse allows them and silently parses input to Version
 70                ThrowHelper.ThrowFormatException(DataType.Version);
 71            }
 72            if (Version.TryParse(versionString, out Version? result))
 73            {
 74                return result;
 75            }
 76#endif
 677            ThrowHelper.ThrowJsonException();
 78            return null;
 079        }
 80
 81        public override void Write(Utf8JsonWriter writer, Version? value, JsonSerializerOptions options)
 082        {
 083            if (value is null)
 084            {
 085                writer.WriteNullValue();
 086                return;
 87            }
 88
 89#if NET
 090            Span<byte> span = stackalloc byte[MaximumVersionLength];
 091            bool formattedSuccessfully = value.TryFormat(span, out int charsWritten);
 092            Debug.Assert(formattedSuccessfully && charsWritten >= MinimumVersionLength);
 093            writer.WriteStringValue(span.Slice(0, charsWritten));
 94#else
 95            writer.WriteStringValue(value.ToString());
 96#endif
 097        }
 98
 99        internal override Version ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOp
 0100        {
 0101            return ReadCore(ref reader);
 0102        }
 103
 104        internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, Version value, JsonSerializerOptions optio
 0105        {
 0106            ArgumentNullException.ThrowIfNull(value);
 107
 108#if NET
 0109            Span<byte> span = stackalloc byte[MaximumVersionLength];
 0110            bool formattedSuccessfully = value.TryFormat(span, out int charsWritten);
 0111            Debug.Assert(formattedSuccessfully && charsWritten >= MinimumVersionLength);
 0112            writer.WritePropertyName(span.Slice(0, charsWritten));
 113#else
 114            writer.WritePropertyName(value.ToString());
 115#endif
 0116        }
 117
 118        internal override JsonSchema? GetSchema(JsonNumberHandling _) =>
 0119            new()
 0120            {
 0121                Type = JsonSchemaType.String,
 0122                Comment = "Represents a version string.",
 0123                Pattern = @"^\d+(\.\d+){1,3}$",
 0124            };
 125    }
 126}