< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Read(...)100%11100%
Write(...)100%11100%
GetClonedOptions(...)100%22100%

File(s)

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

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3using Elsa.Expressions.Contracts;
 4using Elsa.Workflows.Memory;
 5using Elsa.Workflows.Models;
 6using Microsoft.Extensions.Logging;
 7
 8namespace Elsa.Workflows.Serialization.Converters;
 9
 10/// <summary>
 11/// Serializes <see cref="Type"/> objects to a simple alias representing said type.
 12/// </summary>
 13public class VariableConverter : JsonConverter<Variable>
 14{
 15    private readonly VariableMapper _mapper;
 16    private JsonSerializerOptions? _options;
 17
 18    /// <inheritdoc />
 19    // ReSharper disable once ContextualLoggerProblem
 387020    public VariableConverter(IWellKnownTypeRegistry wellKnownTypeRegistry, ILogger<VariableMapper> logger)
 21    {
 387022        _mapper = new VariableMapper(wellKnownTypeRegistry, logger);
 387023    }
 24
 25    /// <inheritdoc />
 26    public override Variable Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 27    {
 33928        var newOptions = GetClonedOptions(options);
 33929        var model = JsonSerializer.Deserialize<VariableModel>(ref reader, newOptions)!;
 33930        var variable = _mapper.Map(model);
 31
 33932        return variable;
 33    }
 34
 35    /// <inheritdoc />
 36    public override void Write(Utf8JsonWriter writer, Variable value, JsonSerializerOptions options)
 37    {
 18838        var model = _mapper.Map(value);
 18839        JsonSerializer.Serialize(writer, model, options);
 18840    }
 41
 42    private JsonSerializerOptions GetClonedOptions(JsonSerializerOptions options)
 43    {
 33944        if(_options != null)
 4945            return _options;
 46
 29047        var newOptions = new JsonSerializerOptions(options);
 29048        newOptions.Converters.Add(new JsonPrimitiveToStringConverter());
 29049        _options = newOptions;
 29050        return newOptions;
 51    }
 52}