< Summary

Information
Class: Elsa.Workflows.Serialization.Converters.JsonPrimitiveToStringConverter
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Converters/JsonPrimitiveToStringConverter.cs
Line coverage
21%
Covered lines: 3
Uncovered lines: 11
Coverable lines: 14
Total lines: 43
Line coverage: 21.4%
Branch coverage
22%
Covered branches: 2
Total branches: 9
Branch coverage: 22.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Read(...)22.22%43925%
Write(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Converters/JsonPrimitiveToStringConverter.cs

#LineLine coverage
 1using System.Globalization;
 2using System.Text.Json;
 3using System.Text.Json.Serialization;
 4
 5namespace Elsa.Workflows.Serialization.Converters;
 6
 7/// <summary>
 8/// Converts primitives to and from JSON strings.
 9/// </summary>
 10public class JsonPrimitiveToStringConverter : JsonConverter<string>
 11{
 12    /// <inheritdoc />
 13    public override string Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 14    {
 152415        switch (reader.TokenType)
 16        {
 17            case JsonTokenType.True:
 018                return "True";
 19            case JsonTokenType.False:
 020                return "False";
 021            case JsonTokenType.Number when reader.TryGetInt64(out var l):
 022                return l.ToString();
 23            case JsonTokenType.Number:
 024                return reader.GetDouble().ToString(CultureInfo.InvariantCulture);
 152425            case JsonTokenType.String when reader.TryGetDateTimeOffset(out var datetime):
 026                return datetime.ToString();
 27            case JsonTokenType.String:
 152428                return reader.GetString()!;
 29            default:
 30            {
 31                // Use JsonElement as fallback.
 032                using var document = JsonDocument.ParseValue(ref reader);
 033                return document.RootElement.Clone().ToString();
 34            }
 35        }
 036    }
 37
 38    /// <inheritdoc />
 39    public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
 40    {
 041        writer.WriteStringValue(value);
 042    }
 43}