< Summary

Information
Class: Elsa.Workflows.Serialization.Serializers.JsonPayloadSerializer
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Serialization/Serializers/JsonPayloadSerializer.cs
Line coverage
75%
Covered lines: 22
Uncovered lines: 7
Coverable lines: 29
Total lines: 91
Line coverage: 75.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%
Serialize(...)100%11100%
SerializeToElement(...)100%210%
Deserialize(...)100%11100%
Deserialize(...)100%210%
Deserialize(...)100%210%
Deserialize(...)100%11100%
Deserialize(...)100%210%
GetOptions()100%11100%
GetService()100%11100%

File(s)

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

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3using System.Text.Json.Serialization.Metadata;
 4using Elsa.Workflows.Serialization.Converters;
 5using Microsoft.Extensions.DependencyInjection;
 6
 7namespace Elsa.Workflows.Serialization.Serializers;
 8
 9/// <summary>
 10/// Serializes simple DTOs from and to JSON.
 11/// </summary>
 12public class JsonPayloadSerializer : IPayloadSerializer
 13{
 14    private readonly IServiceProvider _serviceProvider;
 15
 16    /// <summary>
 17    /// Initializes a new instance of the <see cref="JsonPayloadSerializer"/> class.
 18    /// </summary>
 11419    public JsonPayloadSerializer(IServiceProvider serviceProvider)
 20    {
 11421        _serviceProvider = serviceProvider;
 11422    }
 23
 24    /// <inheritdoc />
 25    public string Serialize(object payload)
 26    {
 744827        var options = GetOptions();
 744828        return JsonSerializer.Serialize(payload, options);
 29    }
 30
 31    /// <inheritdoc />
 32    public JsonElement SerializeToElement(object payload)
 33    {
 034        var options = GetOptions();
 035        return JsonSerializer.SerializeToElement(payload, options);
 36    }
 37
 38    /// <inheritdoc />
 39    public object Deserialize(string payload)
 40    {
 17341        return Deserialize<object>(payload);
 42    }
 43
 44    public object Deserialize(string serializedData, Type type)
 45    {
 046        var options = GetOptions();
 047        return JsonSerializer.Deserialize(serializedData, type, options)!;
 48    }
 49
 50    /// <inheritdoc />
 51    public object Deserialize(JsonElement payload)
 52    {
 053        return Deserialize<object>(payload);
 54    }
 55
 56    /// <inheritdoc />
 57    public T Deserialize<T>(string payload)
 58    {
 400059        var options = GetOptions();
 400060        return JsonSerializer.Deserialize<T>(payload, options)!;
 61    }
 62
 63    /// <inheritdoc />
 64    public T Deserialize<T>(JsonElement payload)
 65    {
 066        var options = GetOptions();
 067        return payload.Deserialize<T>(options)!;
 68    }
 69
 70    /// <inheritdoc />
 71    public JsonSerializerOptions GetOptions()
 72    {
 1146373        var options = new JsonSerializerOptions
 1146374        {
 1146375            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
 1146376            PropertyNameCaseInsensitive = true,
 1146377            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
 1146378        };
 79
 1146380        options.Converters.Add(new JsonStringEnumConverter());
 1146381        options.Converters.Add(JsonMetadataServices.TimeSpanConverter);
 1146382        options.Converters.Add(GetService<PolymorphicObjectConverterFactory>());
 1146383        options.Converters.Add(GetService<TypeJsonConverter>());
 1146384        options.Converters.Add(GetService<VariableConverterFactory>());
 1146385        options.Converters.Add(new FuncExpressionValueConverter());
 1146386        return options;
 87    }
 88
 89    private T GetService<T>()
 3438990        where T : notnull => ActivatorUtilities.GetServiceOrCreateInstance<T>(_serviceProvider);
 91}