< Summary

Information
Class: Elsa.Persistence.EFCore.Converters.PrimitiveDictionaryConverter
Assembly: Elsa.Persistence.EFCore.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.Common/Converters/PrimitiveDictionaryConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 30
Coverable lines: 30
Total lines: 69
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 22
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%4260%
ReadValue(...)0%210140%
Write(...)0%620%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.Common/Converters/PrimitiveDictionaryConverter.cs

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3
 4namespace Elsa.Persistence.EFCore.Converters;
 5
 6public class PrimitiveDictionaryConverter : JsonConverter<IDictionary<string, object>>
 7{
 8    public override IDictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOption
 9    {
 010        if (reader.TokenType != JsonTokenType.StartObject)
 011            throw new JsonException("Expected start of object.");
 12
 013        var dictionary = new Dictionary<string, object>();
 14
 015        while (reader.Read())
 16        {
 017            if (reader.TokenType == JsonTokenType.EndObject)
 018                return dictionary;
 19
 020            var key = reader.GetString()!;
 021            reader.Read();
 022            var value = ReadValue(ref reader, options);
 023            dictionary.Add(key, value);
 24        }
 25
 026        throw new JsonException("Expected end of object.");
 27    }
 28
 29    private object ReadValue(ref Utf8JsonReader reader, JsonSerializerOptions options)
 30    {
 031        switch (reader.TokenType)
 32        {
 33            case JsonTokenType.String:
 034                return reader.GetString()!;
 35            case JsonTokenType.Number:
 036                if (reader.TryGetInt64(out var l))
 037                    return l;
 038                return reader.GetDouble();
 39            case JsonTokenType.True:
 040                return true;
 41            case JsonTokenType.False:
 042                return false;
 43            case JsonTokenType.Null:
 044                return null!;
 45            case JsonTokenType.StartObject:
 046                return JsonSerializer.Deserialize<Dictionary<string, object>>(ref reader, options)!;
 47            case JsonTokenType.StartArray:
 048                return JsonSerializer.Deserialize<List<object>>(ref reader, options)!;
 49            default:
 050                using (var document = JsonDocument.ParseValue(ref reader))
 51                {
 052                    return document.RootElement.Clone().ToString();
 53                }
 54        }
 055    }
 56
 57    public override void Write(Utf8JsonWriter writer, IDictionary<string, object> value, JsonSerializerOptions options)
 58    {
 059        writer.WriteStartObject();
 60
 061        foreach (var kvp in value)
 62        {
 063            writer.WritePropertyName(kvp.Key);
 064            JsonSerializer.Serialize(writer, kvp.Value, options);
 65        }
 66
 067        writer.WriteEndObject();
 068    }
 69}