< Summary

Information
Class: Elsa.Common.Serialization.ConfigurationJsonConverter
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Serialization/ConfigurationJsonConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 22
Coverable lines: 22
Total lines: 45
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
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(...)100%210%
Write(...)0%620%
GetValue(...)0%2040%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Serialization/ConfigurationJsonConverter.cs

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3using Microsoft.Extensions.Configuration;
 4
 5namespace Elsa.Common.Serialization;
 6
 7public class ConfigurationJsonConverter : JsonConverter<IConfiguration>
 8{
 9    public override IConfiguration? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 10    {
 011        var jsonElement = JsonElement.ParseValue(ref reader);
 012        using var memoryStream = new MemoryStream();
 013        using var writer = new Utf8JsonWriter(memoryStream);
 014        jsonElement.WriteTo(writer);
 015        writer.Flush();
 016        memoryStream.Position = 0;
 17
 018        var configurationBuilder = new ConfigurationBuilder();
 019        configurationBuilder.AddJsonStream(memoryStream);
 020        return configurationBuilder.Build();
 021    }
 22
 23    public override void Write(Utf8JsonWriter writer, IConfiguration value, JsonSerializerOptions options)
 24    {
 025        var dictionary = new Dictionary<string, object?>();
 026        foreach (var child in value.GetChildren())
 027            dictionary[child.Key] = GetValue(child);
 28
 029        JsonSerializer.Serialize(writer, dictionary, options);
 030    }
 31
 32    private static object? GetValue(IConfigurationSection section)
 33    {
 034        var children = section.GetChildren().ToList();
 35
 036        if (!children.Any())
 037            return section.Value;
 38
 039        var dict = new Dictionary<string, object?>();
 040        foreach (var child in children)
 041            dict[child.Key] = GetValue(child);
 42
 043        return dict;
 44    }
 45}