| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.Common.Serialization; |
| | | 3 | | using Elsa.Workflows.Serialization.Converters; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Serialization.Serializers; |
| | | 6 | | |
| | | 7 | | /// <inheritdoc cref="IApiSerializer" /> |
| | | 8 | | public class ApiSerializer : ConfigurableSerializer, IApiSerializer |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Initializes a new instance of the <see cref="ApiSerializer"/> class. |
| | | 12 | | /// </summary> |
| | | 13 | | public ApiSerializer(IServiceProvider serviceProvider) |
| | 228 | 14 | | : base(serviceProvider) { } |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public string Serialize(object model) |
| | | 18 | | { |
| | 12 | 19 | | var options = GetOptions(); |
| | 12 | 20 | | return JsonSerializer.Serialize(model, options); |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | 0 | 24 | | public object Deserialize(string serializedModel) => Deserialize<object>(serializedModel); |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public T Deserialize<T>(string serializedModel) |
| | | 28 | | { |
| | 0 | 29 | | var options = GetOptions(); |
| | 0 | 30 | | return JsonSerializer.Deserialize<T>(serializedModel, options)!; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | protected override void Configure(JsonSerializerOptions options) |
| | | 35 | | { |
| | 1 | 36 | | options.PropertyNameCaseInsensitive = true; |
| | 1 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | | 40 | | protected override void AddConverters(JsonSerializerOptions options) |
| | | 41 | | { |
| | 1 | 42 | | options.Converters.Add(CreateInstance<TypeJsonConverter>()); |
| | 1 | 43 | | options.Converters.Add(CreateInstance<FuncExpressionValueConverter>()); |
| | 1 | 44 | | } |
| | | 45 | | |
| | 6 | 46 | | JsonSerializerOptions IApiSerializer.GetOptions() => GetOptions(); |
| | | 47 | | |
| | | 48 | | JsonSerializerOptions IApiSerializer.ApplyOptions(JsonSerializerOptions options) |
| | | 49 | | { |
| | 0 | 50 | | ApplyOptions(options); |
| | 0 | 51 | | return options; |
| | | 52 | | } |
| | | 53 | | } |