< Summary

Information
Class: Elsa.Common.Converters.DecimalJsonConverter
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Converters/DecimalJsonConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 8
Coverable lines: 8
Total lines: 33
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Read(...)0%2040%
Write(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Converters/DecimalJsonConverter.cs

#LineLine coverage
 1using System.Globalization;
 2using System.Text.Json;
 3using System.Text.Json.Serialization;
 4
 5namespace Elsa.Common.Converters;
 6
 7/// <summary>
 8/// Converts decimals to and from JSON strings.
 9/// </summary>
 10public class DecimalJsonConverter : JsonConverter<decimal>
 11{
 12    /// <inheritdoc />
 13    public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 14    {
 015        if (reader.TokenType == JsonTokenType.Number)
 016            return reader.GetDecimal();
 17
 018        if (reader.TokenType == JsonTokenType.String)
 19        {
 020            var value = reader.GetString()!;
 021            return decimal.Parse(value, CultureInfo.InvariantCulture);
 22        }
 23
 024        throw new JsonException("Expected number or string.");
 25    }
 26
 27    /// <inheritdoc />
 28    public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options)
 29    {
 30        // Write the decimal as a JSON number
 031        writer.WriteNumberValue(value);
 032    }
 33}