< Summary

Line coverage
21%
Covered lines: 11
Uncovered lines: 41
Coverable lines: 52
Total lines: 91
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/DoubleConverter.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 DoubleConverter : JsonPrimitiveConverter<double>
 11    {
 112        public DoubleConverter()
 113        {
 114            IsInternalConverterForNumberType = true;
 115        }
 16
 017        internal override bool IsIeeeFloatingPointConverter => true;
 18
 19        public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 820        {
 821            if (options?.NumberHandling is not null and not JsonNumberHandling.Strict)
 022            {
 023                return ReadNumberWithCustomHandling(ref reader, options.NumberHandling, options);
 24            }
 25
 826            return reader.GetDouble();
 427        }
 28
 29        public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options)
 030        {
 031            if (options?.NumberHandling is not null and not JsonNumberHandling.Strict)
 032            {
 033                WriteNumberWithCustomHandling(writer, value, options.NumberHandling);
 034                return;
 35            }
 36
 037            writer.WriteNumberValue(value);
 038        }
 39
 40        internal override double ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOpt
 041        {
 042            Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
 043            return reader.GetDoubleWithQuotes();
 044        }
 45
 46        internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, double value, JsonSerializerOptions option
 047        {
 048            writer.WritePropertyName(value);
 049        }
 50
 51        internal override double ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, Js
 452        {
 453            if (reader.TokenType == JsonTokenType.String)
 054            {
 055                if ((JsonNumberHandling.AllowReadingFromString & handling) != 0)
 056                {
 057                    return reader.GetDoubleWithQuotes();
 58                }
 059                else if ((JsonNumberHandling.AllowNamedFloatingPointLiterals & handling) != 0)
 060                {
 061                    return reader.GetDoubleFloatingPointConstant();
 62                }
 063            }
 64
 465            return reader.GetDouble();
 066        }
 67
 68        internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, double value, JsonNumberHandling han
 069        {
 070            if ((JsonNumberHandling.WriteAsString & handling) != 0)
 071            {
 072                writer.WriteNumberValueAsString(value);
 073            }
 074            else if ((JsonNumberHandling.AllowNamedFloatingPointLiterals & handling) != 0)
 075            {
 076                writer.WriteFloatingPointConstant(value);
 077            }
 78            else
 079            {
 080                writer.WriteNumberValue(value);
 081            }
 082        }
 83
 84        internal override JsonSchema? GetSchema(JsonNumberHandling numberHandling) =>
 085                GetSchemaForNumericType(JsonSchemaType.Number, numberHandling, isIeeeFloatingPoint: true);
 86
 87        internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling numberHandling) =>
 088            GetSupportedJsonValueTypesForNumericType(numberHandling);
 89    }
 90}
 91