< Summary

Information
Line coverage
36%
Covered lines: 18
Uncovered lines: 32
Coverable lines: 50
Total lines: 86
Line coverage: 36%
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%

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\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
 18        public override float Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 17419        {
 17420            if (options?.NumberHandling is not null and not JsonNumberHandling.Strict)
 021            {
 022                return ReadNumberWithCustomHandling(ref reader, options.NumberHandling, options);
 23            }
 24
 17425            return reader.GetSingle();
 2626        }
 27
 28        public override void Write(Utf8JsonWriter writer, float value, JsonSerializerOptions options)
 029        {
 030            if (options?.NumberHandling is not null and not JsonNumberHandling.Strict)
 031            {
 032                WriteNumberWithCustomHandling(writer, value, options.NumberHandling);
 033                return;
 34            }
 35
 036            writer.WriteNumberValue(value);
 037        }
 38
 39        internal override float ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOpti
 040        {
 041            Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
 042            return reader.GetSingleWithQuotes();
 043        }
 44
 45        internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, float value, JsonSerializerOptions options
 046        {
 047            writer.WritePropertyName(value);
 048        }
 49
 50        internal override float ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, Jso
 46051        {
 46052            if (reader.TokenType == JsonTokenType.String)
 17253            {
 17254                if ((JsonNumberHandling.AllowReadingFromString & handling) != 0)
 14055                {
 14056                    return reader.GetSingleWithQuotes();
 57                }
 3258                else if ((JsonNumberHandling.AllowNamedFloatingPointLiterals & handling) != 0)
 059                {
 060                    return reader.GetSingleFloatingPointConstant();
 61                }
 3262            }
 63
 32064            return reader.GetSingle();
 11465        }
 66
 67        internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, float value, JsonNumberHandling hand
 068        {
 069            if ((JsonNumberHandling.WriteAsString & handling) != 0)
 070            {
 071                writer.WriteNumberValueAsString(value);
 072            }
 073            else if ((JsonNumberHandling.AllowNamedFloatingPointLiterals & handling) != 0)
 074            {
 075                writer.WriteFloatingPointConstant(value);
 076            }
 77            else
 078            {
 079                writer.WriteNumberValue(value);
 080            }
 081        }
 82
 83        internal override JsonSchema? GetSchema(JsonNumberHandling numberHandling) =>
 084            GetSchemaForNumericType(JsonSchemaType.Number, numberHandling, isIeeeFloatingPoint: true);
 85    }
 86}