| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using System.Text.Json.Serialization.Metadata; |
| | | 4 | | using Elsa.Workflows.Serialization.Converters; |
| | | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Serialization.Serializers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Serializes simple DTOs from and to JSON. |
| | | 11 | | /// </summary> |
| | | 12 | | public 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> |
| | 114 | 19 | | public JsonPayloadSerializer(IServiceProvider serviceProvider) |
| | | 20 | | { |
| | 114 | 21 | | _serviceProvider = serviceProvider; |
| | 114 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public string Serialize(object payload) |
| | | 26 | | { |
| | 7448 | 27 | | var options = GetOptions(); |
| | 7448 | 28 | | return JsonSerializer.Serialize(payload, options); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public JsonElement SerializeToElement(object payload) |
| | | 33 | | { |
| | 0 | 34 | | var options = GetOptions(); |
| | 0 | 35 | | return JsonSerializer.SerializeToElement(payload, options); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public object Deserialize(string payload) |
| | | 40 | | { |
| | 173 | 41 | | return Deserialize<object>(payload); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public object Deserialize(string serializedData, Type type) |
| | | 45 | | { |
| | 0 | 46 | | var options = GetOptions(); |
| | 0 | 47 | | return JsonSerializer.Deserialize(serializedData, type, options)!; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | | 51 | | public object Deserialize(JsonElement payload) |
| | | 52 | | { |
| | 0 | 53 | | return Deserialize<object>(payload); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public T Deserialize<T>(string payload) |
| | | 58 | | { |
| | 4000 | 59 | | var options = GetOptions(); |
| | 4000 | 60 | | return JsonSerializer.Deserialize<T>(payload, options)!; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | | 64 | | public T Deserialize<T>(JsonElement payload) |
| | | 65 | | { |
| | 0 | 66 | | var options = GetOptions(); |
| | 0 | 67 | | return payload.Deserialize<T>(options)!; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | | 71 | | public JsonSerializerOptions GetOptions() |
| | | 72 | | { |
| | 11463 | 73 | | var options = new JsonSerializerOptions |
| | 11463 | 74 | | { |
| | 11463 | 75 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 11463 | 76 | | PropertyNameCaseInsensitive = true, |
| | 11463 | 77 | | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| | 11463 | 78 | | }; |
| | | 79 | | |
| | 11463 | 80 | | options.Converters.Add(new JsonStringEnumConverter()); |
| | 11463 | 81 | | options.Converters.Add(JsonMetadataServices.TimeSpanConverter); |
| | 11463 | 82 | | options.Converters.Add(GetService<PolymorphicObjectConverterFactory>()); |
| | 11463 | 83 | | options.Converters.Add(GetService<TypeJsonConverter>()); |
| | 11463 | 84 | | options.Converters.Add(GetService<VariableConverterFactory>()); |
| | 11463 | 85 | | options.Converters.Add(new FuncExpressionValueConverter()); |
| | 11463 | 86 | | return options; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | private T GetService<T>() |
| | 34389 | 90 | | where T : notnull => ActivatorUtilities.GetServiceOrCreateInstance<T>(_serviceProvider); |
| | | 91 | | } |