< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 41
Coverable lines: 41
Total lines: 78
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 30
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%26260%
GetSupportedJsonValueTypesForNumericType(...)0%220%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/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 NET11_0_OR_GREATER
 038            Debug.Assert(isIeeeFloatingPoint == (typeof(T) == typeof(double) || typeof(T) == typeof(float) || typeof(T) 
 039                typeof(T) == typeof(System.Numerics.BFloat16) || typeof(T) == typeof(System.Numerics.Decimal32) ||
 040                typeof(T) == typeof(System.Numerics.Decimal64) || typeof(T) == typeof(System.Numerics.Decimal128)));
 41#elif NET
 42            Debug.Assert(isIeeeFloatingPoint == (typeof(T) == typeof(double) || typeof(T) == typeof(float) || typeof(T) 
 43#endif
 044            string? pattern = null;
 45
 046            if ((numberHandling & (JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString)) != 0)
 047            {
 048                pattern = schemaType is JsonSchemaType.Integer
 049                    ? @"^-?(?:0|[1-9]\d*)$"
 050                    : isIeeeFloatingPoint
 051                        ? @"^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$"
 052                        : @"^-?(?:0|[1-9]\d*)(?:\.\d+)?$";
 53
 054                schemaType |= JsonSchemaType.String;
 055            }
 56
 057            if (isIeeeFloatingPoint && (numberHandling & JsonNumberHandling.AllowNamedFloatingPointLiterals) != 0)
 058            {
 059                return new JsonSchema
 060                {
 061                    AnyOf =
 062                    [
 063                        new JsonSchema { Type = schemaType, Pattern = pattern },
 064                        new JsonSchema { Enum = [(JsonNode)"NaN", (JsonNode)"Infinity", (JsonNode)"-Infinity"] },
 065                    ]
 066                };
 67            }
 68
 069            return new JsonSchema { Type = schemaType, Pattern = pattern };
 070        }
 71
 72        private protected static JsonValueType GetSupportedJsonValueTypesForNumericType(JsonNumberHandling numberHandlin
 073            (numberHandling & JsonNumberHandling.AllowReadingFromString) != 0
 074                ? JsonValueType.Number | JsonValueType.String
 075                : JsonValueType.Number;
 76    }
 77}
 78