< Summary

Information
Class: Elsa.Workflows.Serialization.Serializers.SafeSerializer
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Serializers/SafeSerializer.cs
Line coverage
50%
Covered lines: 10
Uncovered lines: 10
Coverable lines: 20
Total lines: 82
Line coverage: 50%
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%
SerializeToElementAsync(...)100%210%
DeserializeAsync(...)100%210%
DeserializeAsync(...)100%210%
Serialize(...)100%11100%
SerializeToElement(...)100%210%
Deserialize(...)100%210%
Deserialize(...)100%210%
AddConverters(...)100%11100%

File(s)

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

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using System.Text.Json;
 3using System.Text.Json.Serialization;
 4using Elsa.Common.Serialization;
 5using Elsa.Expressions.Contracts;
 6using Elsa.Expressions.Services;
 7using Elsa.Workflows.Serialization.Converters;
 8using Microsoft.Extensions.DependencyInjection;
 9
 10namespace Elsa.Workflows.Serialization.Serializers;
 11
 12/// <inheritdoc cref="ISafeSerializer" />
 13public class SafeSerializer : ConfigurableSerializer, ISafeSerializer
 14{
 15    /// <inheritdoc />
 16    public SafeSerializer(IServiceProvider serviceProvider)
 22817        : base(serviceProvider) { }
 18
 19    /// <inheritdoc />
 20    [RequiresUnreferencedCode("The type T may be trimmed.")]
 21    public ValueTask<string> SerializeAsync(object? value, CancellationToken cancellationToken = default)
 22    {
 023        return ValueTask.FromResult(Serialize(value));
 24    }
 25
 26    /// <inheritdoc />
 27    [RequiresUnreferencedCode("The type T may be trimmed.")]
 28    public ValueTask<JsonElement> SerializeToElementAsync(object? value, CancellationToken cancellationToken = default)
 29    {
 030        return new(SerializeToElement(value));
 31    }
 32
 33    /// <inheritdoc />
 34    [RequiresUnreferencedCode("The type T may be trimmed.")]
 35    public ValueTask<T> DeserializeAsync<T>(string json, CancellationToken cancellationToken = default)
 36    {
 037        return new(Deserialize<T>(json));
 38    }
 39
 40    /// <inheritdoc />
 41    [RequiresUnreferencedCode("The type T may be trimmed.")]
 42    public ValueTask<T> DeserializeAsync<T>(JsonElement element, CancellationToken cancellationToken = default)
 43    {
 044        return new(Deserialize<T>(element));
 45    }
 46
 47    public string Serialize(object? value)
 48    {
 214249        var options = GetOptions();
 214250        return JsonSerializer.Serialize(value, options);
 51    }
 52
 53    public JsonElement SerializeToElement(object? value)
 54    {
 055        var options = GetOptions();
 056        return JsonSerializer.SerializeToElement(value, options);
 57    }
 58
 59    public T Deserialize<T>(string json)
 60    {
 061        var options = GetOptions();
 062        return JsonSerializer.Deserialize<T>(json, options)!;
 63    }
 64
 65    public T Deserialize<T>(JsonElement element)
 66    {
 067        var options = GetOptions();
 068        return element.Deserialize<T>(options)!;
 69    }
 70
 71    /// <inheritdoc />
 72    protected override void AddConverters(JsonSerializerOptions options)
 73    {
 11474        var expressionDescriptorRegistry = ServiceProvider.GetRequiredService<IExpressionDescriptorRegistry>();
 75
 11476        options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
 11477        options.Converters.Add(new TypeJsonConverter(WellKnownTypeRegistry.CreateDefault()));
 11478        options.Converters.Add(new SafeValueConverterFactory());
 11479        options.Converters.Add(new ExpressionJsonConverterFactory(expressionDescriptorRegistry));
 11480        options.Converters.Add(new FuncExpressionValueConverter());
 11481    }
 82}