| | | 1 | | using System.Dynamic; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | using Elsa.Common.Serialization; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Serialization.Converters; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// A JSON converter factory that creates <see cref="PolymorphicObjectConverter"/> instances. |
| | | 11 | | /// </summary> |
| | | 12 | | public class PolymorphicObjectConverterFactory : JsonConverterFactory |
| | | 13 | | { |
| | | 14 | | private readonly ISerializationTypeRegistry _workflowJsonTypeRegistry; |
| | | 15 | | private readonly ILogger? _logger; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// A JSON converter factory that creates <see cref="PolymorphicObjectConverter"/> instances. |
| | | 19 | | /// </summary> |
| | 42632 | 20 | | public PolymorphicObjectConverterFactory(ISerializationTypeRegistry workflowJsonTypeRegistry, ILogger? logger = null |
| | | 21 | | { |
| | 42632 | 22 | | _workflowJsonTypeRegistry = workflowJsonTypeRegistry; |
| | 42632 | 23 | | _logger = logger; |
| | 42632 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Default constructor for use with attributes. |
| | | 28 | | /// </summary> |
| | 10094 | 29 | | public PolymorphicObjectConverterFactory() |
| | | 30 | | { |
| | 10094 | 31 | | _workflowJsonTypeRegistry = SerializationTypeRegistry.CreateDefault(); |
| | 10094 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | | 35 | | public override bool CanConvert(Type typeToConvert) |
| | | 36 | | { |
| | 239828 | 37 | | if (typeToConvert.IsClass |
| | 239828 | 38 | | && typeToConvert == typeof(object) |
| | 239828 | 39 | | || typeToConvert == typeof(ExpandoObject) |
| | 239828 | 40 | | || typeToConvert == typeof(Dictionary<string, object>)) |
| | 20989 | 41 | | return true; |
| | | 42 | | |
| | 218839 | 43 | | if (typeToConvert.IsInterface |
| | 218839 | 44 | | && typeToConvert == typeof(IDictionary<string, object>)) |
| | 28737 | 45 | | return true; |
| | | 46 | | |
| | 190102 | 47 | | return false; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | | 51 | | public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) |
| | | 52 | | { |
| | 88420 | 53 | | if (typeof(IDictionary<string, object>).IsAssignableFrom(typeToConvert)) |
| | 36329 | 54 | | return new PolymorphicDictionaryConverter(options, _workflowJsonTypeRegistry); |
| | | 55 | | |
| | 52091 | 56 | | return new PolymorphicObjectConverter(_workflowJsonTypeRegistry, _logger); |
| | | 57 | | } |
| | | 58 | | } |