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