< Summary

Line coverage
6%
Covered lines: 2
Uncovered lines: 27
Coverable lines: 29
Total lines: 58
Line coverage: 6.8%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
Read(...)100%1166.66%
Write(...)0%220%
ReadAsPropertyNameCore(...)100%110%
WriteAsPropertyNameCore(...)0%660%
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/StringConverter.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 StringConverter : JsonPrimitiveConverter<string?>
 11    {
 12        public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 1213        {
 1214            return reader.GetString();
 015        }
 16
 17        public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options)
 018        {
 19            // For performance, lift up the writer implementation.
 020            if (value is null)
 021            {
 022                writer.WriteNullValue();
 023            }
 24            else
 025            {
 026                writer.WriteStringValue(value.AsSpan());
 027            }
 028        }
 29
 30        internal override string ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOpt
 031        {
 032            Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
 033            return reader.GetString()!;
 034        }
 35
 36        internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, string value, JsonSerializerOptions option
 037        {
 038            ArgumentNullException.ThrowIfNull(value);
 39
 040            if (options.DictionaryKeyPolicy is not null && !isWritingExtensionDataProperty)
 041            {
 042                value = options.DictionaryKeyPolicy.ConvertName(value);
 43
 044                if (value is null)
 045                {
 046                    ThrowHelper.ThrowInvalidOperationException_NamingPolicyReturnNull(options.DictionaryKeyPolicy);
 47                }
 048            }
 49
 050            writer.WritePropertyName(value);
 051        }
 52
 053        internal override JsonSchema? GetSchema(JsonNumberHandling _) => new() { Type = JsonSchemaType.String };
 54
 055        internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling _) => JsonValueType.String;
 56    }
 57}
 58