< Summary

Line coverage
28%
Covered lines: 12
Uncovered lines: 30
Coverable lines: 42
Total lines: 78
Line coverage: 28.5%
Branch coverage
27%
Covered branches: 5
Total branches: 18
Branch coverage: 27.7%
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(...)50%4457.14%
WriteNumberWithCustomHandling(...)0%220%
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/DecimalConverter.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.Schema;
 6
 7namespace System.Text.Json.Serialization.Converters
 8{
 9    internal sealed class DecimalConverter : JsonPrimitiveConverter<decimal>
 10    {
 111        public DecimalConverter()
 112        {
 113            IsInternalConverterForNumberType = true;
 114        }
 15
 16        public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 817        {
 818            if (options?.NumberHandling is not null and not JsonNumberHandling.Strict)
 019            {
 020                return ReadNumberWithCustomHandling(ref reader, options.NumberHandling, options);
 21            }
 22
 823            return reader.GetDecimal();
 424        }
 25
 26        public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options)
 027        {
 028            if (options?.NumberHandling is not null and not JsonNumberHandling.Strict)
 029            {
 030                WriteNumberWithCustomHandling(writer, value, options.NumberHandling);
 031                return;
 32            }
 33
 034            writer.WriteNumberValue(value);
 035        }
 36
 37        internal override decimal ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOp
 038        {
 039            Debug.Assert(reader.TokenType == JsonTokenType.PropertyName);
 040            return reader.GetDecimalWithQuotes();
 041        }
 42
 43        internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, decimal value, JsonSerializerOptions optio
 044        {
 045            writer.WritePropertyName(value);
 046        }
 47
 48        internal override decimal ReadNumberWithCustomHandling(ref Utf8JsonReader reader, JsonNumberHandling handling, J
 449        {
 450            if (reader.TokenType == JsonTokenType.String &&
 451                (JsonNumberHandling.AllowReadingFromString & handling) != 0)
 052            {
 053                return reader.GetDecimalWithQuotes();
 54            }
 55
 456            return reader.GetDecimal();
 057        }
 58
 59        internal override void WriteNumberWithCustomHandling(Utf8JsonWriter writer, decimal value, JsonNumberHandling ha
 060        {
 061            if ((JsonNumberHandling.WriteAsString & handling) != 0)
 062            {
 063                writer.WriteNumberValueAsString(value);
 064            }
 65            else
 066            {
 067                writer.WriteNumberValue(value);
 068            }
 069        }
 70
 71        internal override JsonSchema? GetSchema(JsonNumberHandling numberHandling) =>
 072            GetSchemaForNumericType(JsonSchemaType.Number, numberHandling);
 73
 74        internal override JsonValueType GetSupportedJsonValueTypes(JsonNumberHandling numberHandling) =>
 075            GetSupportedJsonValueTypesForNumericType(numberHandling);
 76    }
 77}
 78