< Summary

Line coverage
21%
Covered lines: 11
Uncovered lines: 41
Coverable lines: 52
Total lines: 92
Line coverage: 21.1%
Branch coverage
18%
Covered branches: 4
Total branches: 22
Branch coverage: 18.1%
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(...)16.66%6625%
WriteNumberWithCustomHandling(...)0%440%
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/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)
 821        {
 822            if (options?.NumberHandling is not null and not JsonNumberHandling.Strict)
 023            {
 024                return ReadNumberWithCustomHandling(ref reader, options.NumberHandling, options);
 25            }
 26
 827            return reader.GetSingle();
 428        }
 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
 453        {
 454            if (reader.TokenType == JsonTokenType.String)
 055            {
 056                if ((JsonNumberHandling.AllowReadingFromString & handling) != 0)
 057                {
 058                    return reader.GetSingleWithQuotes();
 59                }
 060                else if ((JsonNumberHandling.AllowNamedFloatingPointLiterals & handling) != 0)
 061                {
 062                    return reader.GetSingleFloatingPointConstant();
 63                }
 064            }
 65
 466            return reader.GetSingle();
 067        }
 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}
 92