< Summary

Information
Class: Elsa.Common.Converters.NullableBooleanConverter
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Converters/NullableBooleanConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 12
Coverable lines: 12
Total lines: 34
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
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%7280%
Write(...)0%620%

File(s)

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

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3
 4namespace Elsa.Common.Converters;
 5
 6public class NullableBooleanConverter : JsonConverter<bool?>
 7{
 8    public override bool? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 9    {
 010        switch (reader.TokenType)
 11        {
 12            case JsonTokenType.True:
 013                return true;
 14            case JsonTokenType.False:
 015                return false;
 16            case JsonTokenType.String:
 017                var value = reader.GetString();
 018                if (bool.TryParse(value, out var b))
 019                    return b;
 20                break;
 21            case JsonTokenType.Null:
 022                return null;
 23        }
 024        throw new JsonException($"Cannot convert {reader.TokenType} to bool?");
 25    }
 26
 27    public override void Write(Utf8JsonWriter writer, bool? value, JsonSerializerOptions options)
 28    {
 029        if (value.HasValue)
 030            writer.WriteBooleanValue(value.Value);
 31        else
 032            writer.WriteNullValue();
 033    }
 34}