< Summary

Information
Class: Elsa.Workflows.Serialization.Converters.ExcludeFromHashConverterFactory
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Converters/ExcludeFromHashConverter.cs
Line coverage
100%
Covered lines: 2
Uncovered lines: 0
Coverable lines: 2
Total lines: 65
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
CanConvert(...)100%11100%
CreateConverter(...)100%11100%

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    {
 19        throw new NotSupportedException();
 20    }
 21
 22    /// <inheritdoc />
 23    public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
 24    {
 25        writer.WriteStartObject();
 26        var newOptions = GetClonedOptions(options);
 27
 28        foreach (var property in value.GetType().GetProperties())
 29        {
 30            var attribute = property.GetCustomAttribute<ExcludeFromHashAttribute>();
 31
 32            if (attribute != null)
 33            {
 34                continue;
 35            }
 36
 37            writer.WritePropertyName(property.Name);
 38            JsonSerializer.Serialize(writer, property.GetValue(value), newOptions);
 39        }
 40
 41        writer.WriteEndObject();
 42    }
 43
 44    private JsonSerializerOptions GetClonedOptions(JsonSerializerOptions options)
 45    {
 46        if(_options != null)
 47            return _options;
 48
 49        var newOptions = new JsonSerializerOptions(options);
 50        newOptions.Converters.RemoveWhere(x => x is ExcludeFromHashConverterFactory);
 51        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 />
 1561    public override bool CanConvert(Type typeToConvert) => true;
 62
 63    /// <inheritdoc />
 1564    public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) => new ExcludeFromH
 65}