< Summary

Information
Line coverage
30%
Covered lines: 8
Uncovered lines: 18
Coverable lines: 26
Total lines: 56
Line coverage: 30.7%
Branch coverage
33%
Covered branches: 2
Total branches: 6
Branch coverage: 33.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Read(...)50%22100%
Write(...)0%220%
ReadAsPropertyNameCore(...)100%110%
ReadCore(...)50%2271.42%
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\UriConverter.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 UriConverter : JsonPrimitiveConverter<Uri?>
 11    {
 12        public override Uri? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 63413        {
 63414            return reader.TokenType is JsonTokenType.Null ? null : ReadCore(ref reader);
 17615        }
 16
 17        public override void Write(Utf8JsonWriter writer, Uri? value, JsonSerializerOptions options)
 018        {
 019            if (value is null)
 020            {
 021                writer.WriteNullValue();
 022                return;
 23            }
 24
 025            writer.WriteStringValue(value.OriginalString);
 026        }
 27
 28        internal override Uri ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOption
 029        {
 030            Debug.Assert(reader.TokenType is JsonTokenType.PropertyName);
 031            return ReadCore(ref reader);
 032        }
 33
 34        private static Uri ReadCore(ref Utf8JsonReader reader)
 63435        {
 63436            string? uriString = reader.GetString();
 37
 17638            if (!Uri.TryCreate(uriString, UriKind.RelativeOrAbsolute, out Uri? value))
 039            {
 040                ThrowHelper.ThrowJsonException();
 41            }
 42
 17643            return value;
 17644        }
 45
 46        internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, Uri value, JsonSerializerOptions options, 
 047        {
 048            ArgumentNullException.ThrowIfNull(value);
 49
 050            writer.WritePropertyName(value.OriginalString);
 051        }
 52
 53        internal override JsonSchema? GetSchema(JsonNumberHandling _) =>
 054            new() { Type = JsonSchemaType.String, Format = "uri" };
 55    }
 56}