< Summary

Information
Class: System.Text.Json.Serialization.Converters.SingleConverter
Assembly: System.Text.Json
File(s): D:\runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\Converters\Value\SingleConverter.cs
Line coverage
34%
Covered lines: 18
Uncovered lines: 34
Coverable lines: 52
Total lines: 91
Line coverage: 34.6%
Branch coverage
36%
Covered branches: 8
Total branches: 22
Branch coverage: 36.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
Read(...)50%6666.66%
Write(...)0%660%
ReadAsPropertyNameCore(...)100%110%
WriteAsPropertyNameCore(...)100%110%
ReadNumberWithCustomHandling(...)83.33%6683.33%
WriteNumberWithCustomHandling(...)0%440%
GetSchema(...)100%110%
GetSupportedJsonValueTypes(...)100%110%

File(s)

D:\runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Serialization\Converters\Value\SingleConverter.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 SingleConverter : JsonPrimitiveConverter<float>
 11    {
 12
 113        public SingleConverter()
 114        {
 115            IsInternalConverterForNumberType = true;
 116        }
 17
 018        internal override bool IsIeeeFloatingPointConverter => true;
 19
 20        public override float Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 17621        {
 17622            if (options?.NumberHandling is not null and not JsonNumberHandling.Strict)
 023            {
 024                return ReadNumberWithCustomHandling(ref reader, options.NumberHandling, options);
 25            }
 26
 17627            return reader.GetSingle();
 5228        }
 29
 30        public override void Write(Utf8JsonWriter writer, float value, JsonSerializerOptions options)
 031        {
 032            if (options?.NumberHandling is not null and not JsonNumberHandling.Strict)
 033            {
 034                WriteNumberWithCustomHandling(writer, value, options.NumberHandling);
 035                return;
 36            }
 37
 038            writer.WriteNumberValue(value);
 039        }
 40
 41        internal override float ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOpti
 042        {
 043            Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
 044            return reader.GetSingleWithQuotes();
 045        }
 46
 47        internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, float value, JsonSerializerOptions options
 048        {
 049            writer.WritePropertyName(value);
 050        }
 51
 52        internal override float ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, Jso
 20053        {
 20054            if (reader.TokenType == JsonTokenType.String)
 5655            {
 5656                if ((JsonNumberHandling.AllowReadingFromString & handling) != 0)
 5257                {
 5258                    return reader.GetSingleWithQuotes();
 59                }
 460                else if ((JsonNumberHandling.AllowNamedFloatingPointLiterals & handling) != 0)
 061                {
 062                    return reader.GetSingleFloatingPointConstant();
 63                }
 464            }
 65
 14866            return reader.GetSingle();
 1667        }
 68
 69        internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, float value, JsonNumberHandling hand
 070        {
 071            if ((JsonNumberHandling.WriteAsString & handling) != 0)
 072            {
 073                writer.WriteNumberValueAsString(value);
 074            }
 075            else if ((JsonNumberHandling.AllowNamedFloatingPointLiterals & handling) != 0)
 076            {
 077                writer.WriteFloatingPointConstant(value);
 078            }
 79            else
 080            {
 081                writer.WriteNumberValue(value);
 082            }
 083        }
 84
 85        internal override JsonSchema? GetSchema(JsonNumberHandling numberHandling) =>
 086            GetSchemaForNumericType(JsonSchemaType.Number, numberHandling, isIeeeFloatingPoint: true);
 87
 88        internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling numberHandling) =>
 089            GetSupportedJsonValueTypesForNumericType(numberHandling);
 90    }
 91}