< Summary

Information
Line coverage
37%
Covered lines: 14
Uncovered lines: 23
Coverable lines: 37
Total lines: 67
Line coverage: 37.8%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Read(...)100%1010100%
Write(...)100%110%
ReadAsPropertyNameCore(...)100%110%
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\CharConverter.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.Schema;
 6
 7namespace System.Text.Json.Serialization.Converters
 8{
 9    internal sealed class CharConverter : JsonPrimitiveConverter<char>
 10    {
 11        private const int MaxEscapedCharacterLength = JsonConstants.MaxExpansionFactorWhileEscaping;
 12
 13        public override char Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 64814        {
 64815            if (reader.TokenType is not (JsonTokenType.String or JsonTokenType.PropertyName))
 36416            {
 36417                ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType);
 18            }
 19
 28420            if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, 1, MaxEscapedCharacterLength))
 16821            {
 16822                ThrowHelper.ThrowInvalidOperationException_ExpectedChar(reader.TokenType);
 23            }
 24
 11625            Span<char> buffer = stackalloc char[MaxEscapedCharacterLength];
 11626            int charsWritten = reader.CopyString(buffer);
 27
 5228            if (charsWritten != 1)
 2629            {
 2630                ThrowHelper.ThrowInvalidOperationException_ExpectedChar(reader.TokenType);
 31            }
 32
 2633            return buffer[0];
 2634        }
 35
 36        public override void Write(Utf8JsonWriter writer, char value, JsonSerializerOptions options)
 037        {
 038            writer.WriteStringValue(
 039#if NET
 040                new ReadOnlySpan<char>(in value)
 041#else
 042                value.ToString()
 043#endif
 044                );
 045        }
 46
 47        internal override char ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptio
 048        {
 049            Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
 050            return Read(ref reader, typeToConvert, options);
 051        }
 52
 53        internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, char value, JsonSerializerOptions options,
 054        {
 055            writer.WritePropertyName(
 056#if NET
 057                new ReadOnlySpan<char>(in value)
 058#else
 059                value.ToString()
 060#endif
 061                );
 062        }
 63
 64        internal override JsonSchema? GetSchema(JsonNumberHandling _) =>
 065            new() { Type = JsonSchemaType.String, MinLength = 1, MaxLength = 1 };
 66    }
 67}