< Summary

Information
Class: Elsa.Workflows.Serialization.Converters.ExcludeFromHashConverter
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Converters/ExcludeFromHashConverter.cs
Line coverage
93%
Covered lines: 14
Uncovered lines: 1
Coverable lines: 15
Total lines: 65
Line coverage: 93.3%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
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(...)100%44100%
GetClonedOptions(...)100%22100%

File(s)

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

#LineLine coverage
 1using System.Reflection;
 2using System.Text.Json;
 3using System.Text.Json.Serialization;
 4using Elsa.Extensions;
 5using Elsa.Workflows.Attributes;
 6
 7namespace Elsa.Workflows.Serialization.Converters;
 8
 9/// <summary>
 10/// Serializes an object to JSON, excluding properties marked with <see cref="ExcludeFromHashAttribute"/>.
 11/// </summary>
 12public class ExcludeFromHashConverter : JsonConverter<object>
 13{
 14    private JsonSerializerOptions? _options;
 15
 16    /// <inheritdoc />
 17    public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 18    {
 019        throw new NotSupportedException();
 20    }
 21
 22    /// <inheritdoc />
 23    public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
 24    {
 431425        writer.WriteStartObject();
 431426        var newOptions = GetClonedOptions(options);
 27
 11694628        foreach (var property in value.GetType().GetProperties())
 29        {
 5415930            var attribute = property.GetCustomAttribute<ExcludeFromHashAttribute>();
 31
 5415932            if (attribute != null)
 33            {
 34                continue;
 35            }
 36
 5306437            writer.WritePropertyName(property.Name);
 5306438            JsonSerializer.Serialize(writer, property.GetValue(value), newOptions);
 39        }
 40
 431441        writer.WriteEndObject();
 431442    }
 43
 44    private JsonSerializerOptions GetClonedOptions(JsonSerializerOptions options)
 45    {
 431446        if(_options != null)
 430047            return _options;
 48
 1449        var newOptions = new JsonSerializerOptions(options);
 4250        newOptions.Converters.RemoveWhere(x => x is ExcludeFromHashConverterFactory);
 1451        return _options = newOptions;
 52    }
 53}
 54
 55/// <summary>
 56/// A factory for creating <see cref="ExcludeFromHashConverter"/> instances.
 57/// </summary>
 58public class ExcludeFromHashConverterFactory : JsonConverterFactory
 59{
 60    /// <inheritdoc />
 61    public override bool CanConvert(Type typeToConvert) => true;
 62
 63    /// <inheritdoc />
 64    public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) => new ExcludeFromH
 65}