< Summary

Information
Class: System.Text.Json.Serialization.Converters.JsonPrimitiveConverter<T>
Assembly: System.Text.Json
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\JsonPrimitiveConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 36
Coverable lines: 36
Total lines: 68
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 22
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
WriteAsPropertyName(...)100%110%
ReadAsPropertyName(...)0%220%
GetSchemaForNumericType(...)0%20200%

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\JsonPrimitiveConverter.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.Diagnostics.CodeAnalysis;
 6using System.Text.Json.Nodes;
 7using System.Text.Json.Schema;
 8
 9namespace System.Text.Json.Serialization.Converters
 10{
 11    /// <summary>
 12    /// Inherited by built-in converters serializing types as JSON primitives that support property name serialization.
 13    /// </summary>
 14    internal abstract class JsonPrimitiveConverter<T> : JsonConverter<T>
 15    {
 16        public sealed override void WriteAsPropertyName(Utf8JsonWriter writer, [DisallowNull] T value, JsonSerializerOpt
 017        {
 018            ArgumentNullException.ThrowIfNull(value);
 19
 020            WriteAsPropertyNameCore(writer, value, options, isWritingExtensionDataProperty: false);
 021        }
 22
 23        public sealed override T ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions
 024        {
 025            if (reader.TokenType != JsonTokenType.PropertyName)
 026            {
 027                ThrowHelper.ThrowInvalidOperationException_ExpectedPropertyName(reader.TokenType);
 28            }
 29
 030            return ReadAsPropertyNameCore(ref reader, typeToConvert, options);
 031        }
 32
 33        private protected static JsonSchema GetSchemaForNumericType(JsonSchemaType schemaType, JsonNumberHandling number
 034        {
 035            Debug.Assert(schemaType is JsonSchemaType.Integer or JsonSchemaType.Number);
 036            Debug.Assert(!isIeeeFloatingPoint || schemaType is JsonSchemaType.Number);
 37#if NET
 038            Debug.Assert(isIeeeFloatingPoint == (typeof(T) == typeof(double) || typeof(T) == typeof(float) || typeof(T) 
 39#endif
 040            string? pattern = null;
 41
 042            if ((numberHandling & (JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)) != 0)
 043            {
 044                pattern = schemaType is JsonSchemaType.Integer
 045                    ? @"^-?(?:0|[1-9]\d*)$"
 046                    : isIeeeFloatingPoint
 047                        ? @"^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$"
 048                        : @"^-?(?:0|[1-9]\d*)(?:\.\d+)?$";
 49
 050                schemaType |= JsonSchemaType.String;
 051            }
 52
 053            if (isIeeeFloatingPoint && (numberHandling & JsonNumberHandling.AllowNamedFloatingPointLiterals) != 0)
 054            {
 055                return new JsonSchema
 056                {
 057                    AnyOf =
 058                    [
 059                        new JsonSchema { Type = schemaType, Pattern = pattern },
 060                        new JsonSchema { Enum = [(JsonNode)"NaN", (JsonNode)"Infinity", (JsonNode)"-Infinity"] },
 061                    ]
 062                };
 63            }
 64
 065            return new JsonSchema { Type = schemaType, Pattern = pattern };
 066        }
 67    }
 68}