| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.Expressions.Contracts; |
| | | 3 | | using Elsa.Workflows.Serialization.Converters; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Serialization.Serializers; |
| | | 6 | | |
| | | 7 | | /// <inheritdoc /> |
| | | 8 | | public class BookmarkPayloadSerializer : IBookmarkPayloadSerializer |
| | | 9 | | { |
| | | 10 | | private readonly JsonSerializerOptions _settings; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Initializes a new instance of the <see cref="BookmarkPayloadSerializer"/> class. |
| | | 14 | | /// </summary> |
| | 0 | 15 | | public BookmarkPayloadSerializer(IWellKnownTypeRegistry wellKnownTypeRegistry) |
| | | 16 | | { |
| | 0 | 17 | | _settings = new JsonSerializerOptions |
| | 0 | 18 | | { |
| | 0 | 19 | | // Enables serialization of ValueTuples, which use fields instead of properties. |
| | 0 | 20 | | IncludeFields = true, |
| | 0 | 21 | | PropertyNameCaseInsensitive = true, |
| | 0 | 22 | | }; |
| | | 23 | | |
| | 0 | 24 | | _settings.Converters.Add(new TypeJsonConverter(wellKnownTypeRegistry)); |
| | 0 | 25 | | _settings.Converters.Add(new FuncExpressionValueConverter()); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public T Deserialize<T>(string json) |
| | 0 | 30 | | where T : notnull => JsonSerializer.Deserialize<T>(json, _settings)!; |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | 0 | 33 | | public object Deserialize(string json, Type type) => JsonSerializer.Deserialize(json, type, _settings)!; |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public string Serialize<T>(T payload) |
| | 0 | 37 | | where T : notnull => JsonSerializer.Serialize(payload, payload.GetType(), _settings); |
| | | 38 | | } |