< Summary

Line coverage
14%
Covered lines: 4
Uncovered lines: 23
Coverable lines: 27
Total lines: 59
Line coverage: 14.8%
Branch coverage
16%
Covered branches: 1
Total branches: 6
Branch coverage: 16.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Read(...)50%2266.66%
Write(...)0%220%
ReadAsPropertyNameCore(...)100%110%
ReadCore(...)0%2228.57%
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/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)
 1213        {
 1214            return reader.TokenType is JsonTokenType.Null ? null : ReadCore(ref reader);
 015        }
 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)
 1235        {
 1236            string? uriString = reader.GetString();
 37
 038            if (!Uri.TryCreate(uriString, UriKind.RelativeOrAbsolute, out Uri? value))
 039            {
 040                ThrowHelper.ThrowJsonException();
 41            }
 42
 043            return value;
 044        }
 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
 056        internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.String;
 57    }
 58}
 59