< Summary

Line coverage
10%
Covered lines: 4
Uncovered lines: 34
Coverable lines: 38
Total lines: 70
Line coverage: 10.5%
Branch coverage
25%
Covered branches: 2
Total branches: 8
Branch coverage: 25%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Read(...)25%8828.57%
Write(...)100%110%
ReadAsPropertyNameCore(...)100%110%
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/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 unsafe char Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 1214        {
 1215            if (reader.TokenType is not (JsonTokenType.String or JsonTokenType.PropertyName))
 1216            {
 1217                ThrowHelper.ThrowInvalidOperationException_ExpectedString(reader.TokenType);
 18            }
 19
 020            if (!JsonHelpers.IsInRangeInclusive(reader.ValueLength, 1, MaxEscapedCharacterLength))
 021            {
 022                ThrowHelper.ThrowInvalidOperationException_ExpectedChar(reader.TokenType);
 23            }
 24
 025            Span<char> buffer = stackalloc char[MaxEscapedCharacterLength];
 026            int charsWritten = reader.CopyString(buffer);
 27
 028            if (charsWritten != 1)
 029            {
 030                ThrowHelper.ThrowInvalidOperationException_ExpectedChar(reader.TokenType);
 31            }
 32
 033            return buffer[0];
 034        }
 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
 067        internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.String;
 68    }
 69}
 70