< Summary

Information
Class: Elsa.Workflows.Serialization.Serializers.JsonWorkflowStateSerializer
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Serializers/JsonWorkflowStateSerializer.cs
Line coverage
46%
Covered lines: 15
Uncovered lines: 17
Coverable lines: 32
Total lines: 143
Line coverage: 46.8%
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
.ctor(...)100%11100%
SerializeAsync(...)100%210%
SerializeToUtfBytesAsync(...)100%210%
SerializeToElementAsync(...)100%210%
SerializeAsync(...)100%210%
DeserializeAsync(...)100%210%
DeserializeAsync(...)100%210%
DeserializeAsync(...)100%210%
Serialize(...)100%11100%
SerializeToUtfBytes(...)100%210%
SerializeToElement(...)100%210%
Serialize(...)100%210%
Deserialize(...)100%11100%
Deserialize(...)100%210%
Deserialize(...)100%210%
GetOptions()100%11100%
AddConverters(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Serializers/JsonWorkflowStateSerializer.cs

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using System.Text.Json;
 3using Elsa.Common.Serialization;
 4using Elsa.Expressions.Contracts;
 5using Elsa.Workflows.Serialization.Converters;
 6using Elsa.Workflows.Serialization.ReferenceHandlers;
 7using Elsa.Workflows.State;
 8using Microsoft.Extensions.Logging;
 9
 10namespace Elsa.Workflows.Serialization.Serializers;
 11
 12/// <summary>
 13/// Serializes and deserializes workflow states from and to JSON.
 14/// </summary>
 15public class JsonWorkflowStateSerializer : ConfigurableSerializer, IWorkflowStateSerializer
 16{
 17    private readonly IWellKnownTypeRegistry _wellKnownTypeRegistry;
 18    private readonly ILoggerFactory _loggerFactory;
 19
 20    /// <summary>
 21    /// Initializes a new instance of the <see cref="JsonWorkflowStateSerializer"/> class.
 22    /// </summary>
 23    public JsonWorkflowStateSerializer(IServiceProvider serviceProvider, IWellKnownTypeRegistry wellKnownTypeRegistry, I
 11424        : base(serviceProvider)
 25    {
 11426        _wellKnownTypeRegistry = wellKnownTypeRegistry;
 11427        _loggerFactory = loggerFactory;
 11428    }
 29
 30    /// <inheritdoc />
 31    [RequiresUnreferencedCode("The type 'T' may be trimmed from the output. The serialization process may require access
 32    [Obsolete("Use the non-async version Serialize instead.")]
 33    public Task<string> SerializeAsync(WorkflowState workflowState, CancellationToken cancellationToken = default)
 34    {
 035        return Task.FromResult(Serialize(workflowState));
 36    }
 37
 38    /// <inheritdoc />
 39    [RequiresUnreferencedCode("The type 'T' may be trimmed from the output. The serialization process may require access
 40    [Obsolete("Use the non-async version SerializeToUtfBytes instead.")]
 41    public Task<byte[]> SerializeToUtfBytesAsync(WorkflowState workflowState, CancellationToken cancellationToken = defa
 42    {
 043        return Task.FromResult(SerializeToUtfBytes(workflowState));
 44    }
 45
 46    /// <inheritdoc />
 47    [RequiresUnreferencedCode("The type 'T' may be trimmed from the output. The serialization process may require access
 48    [Obsolete("Use the non-async version SerializeToElement instead.")]
 49    public Task<JsonElement> SerializeToElementAsync(WorkflowState workflowState, CancellationToken cancellationToken = 
 50    {
 051        return Task.FromResult(SerializeToElement(workflowState));
 52    }
 53
 54    /// <inheritdoc />
 55    [RequiresUnreferencedCode("The type 'T' may be trimmed from the output. The serialization process may require access
 56    [Obsolete("Use the non-async version Serialize instead.")]
 57    public Task<string> SerializeAsync(object workflowState, CancellationToken cancellationToken = default)
 58    {
 059        return Task.FromResult(Serialize(workflowState));
 60    }
 61
 62    /// <inheritdoc />
 63    [RequiresUnreferencedCode("The type 'T' may be trimmed from the output. The deserialization process may require acce
 64    [Obsolete("Use the non-async version Deserialize instead.")]
 65    public Task<WorkflowState> DeserializeAsync(string serializedState, CancellationToken cancellationToken = default)
 66    {
 067        return Task.FromResult(Deserialize(serializedState));
 68    }
 69
 70    /// <inheritdoc />
 71    [RequiresUnreferencedCode("The type 'T' may be trimmed from the output. The deserialization process may require acce
 72    [Obsolete("Use the non-async version Deserialize instead.")]
 73    public Task<WorkflowState> DeserializeAsync(JsonElement serializedState, CancellationToken cancellationToken = defau
 74    {
 075        return Task.FromResult(Deserialize(serializedState));
 76    }
 77
 78    /// <inheritdoc />
 79    [RequiresUnreferencedCode("The type 'T' may be trimmed from the output. The deserialization process may require acce
 80    [Obsolete("Use the non-async version Deserialize instead.")]
 81    public Task<T> DeserializeAsync<T>(string serializedState, CancellationToken cancellationToken = default)
 82    {
 083        return Task.FromResult(Deserialize<T>(serializedState));
 84    }
 85
 86    public string Serialize(WorkflowState workflowState)
 87    {
 34988        var options = GetOptions();
 34989        return JsonSerializer.Serialize(workflowState, options);
 90    }
 91
 92    public byte[] SerializeToUtfBytes(WorkflowState workflowState)
 93    {
 094        var options = GetOptions();
 095        return JsonSerializer.SerializeToUtf8Bytes(workflowState, options);
 96    }
 97
 98    public JsonElement SerializeToElement(WorkflowState workflowState)
 99    {
 0100        var options = GetOptions();
 0101        return JsonSerializer.SerializeToElement(workflowState, options);
 102    }
 103
 104    public string Serialize(object workflowState)
 105    {
 0106        var options = GetOptions();
 0107        return JsonSerializer.Serialize(workflowState, workflowState.GetType(), options);
 108    }
 109
 110    public WorkflowState Deserialize(string serializedState)
 111    {
 95112        var options = GetOptions();
 95113        return JsonSerializer.Deserialize<WorkflowState>(serializedState, options)!;
 114    }
 115
 116    public WorkflowState Deserialize(JsonElement serializedState)
 117    {
 0118        var options = GetOptions();
 0119        return serializedState.Deserialize<WorkflowState>(options)!;
 120    }
 121
 122    public T Deserialize<T>(string serializedState)
 123    {
 0124        var options = GetOptions();
 0125        return JsonSerializer.Deserialize<T>(serializedState, options)!;
 126    }
 127
 128    /// <inheritdoc />
 129    public override JsonSerializerOptions GetOptions()
 130    {
 444131        var options = base.GetOptions();
 444132        return new(options) { ReferenceHandler = new CrossScopedReferenceHandler() };
 133    }
 134
 135    /// <inheritdoc />
 136    protected override void AddConverters(JsonSerializerOptions options)
 137    {
 1138        options.Converters.Add(new TypeJsonConverter(_wellKnownTypeRegistry));
 1139        options.Converters.Add(new PolymorphicObjectConverterFactory(_wellKnownTypeRegistry));
 1140        options.Converters.Add(new VariableConverterFactory(_wellKnownTypeRegistry, _loggerFactory));
 1141        options.Converters.Add(new FuncExpressionValueConverter());
 1142    }
 143}