| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using System.Dynamic; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using System.Text.Json.Nodes; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Common.Serialization; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Options for serialization type identifiers. |
| | | 11 | | /// </summary> |
| | | 12 | | public class SerializationTypeOptions |
| | | 13 | | { |
| | 10165 | 14 | | private readonly IDictionary<string, Type> _aliasTypeDictionary = new Dictionary<string, Type>(StringComparer.Ordina |
| | 10165 | 15 | | private readonly IDictionary<Type, string> _typeAliasDictionary = new Dictionary<Type, string>(); |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="SerializationTypeOptions"/> class. |
| | | 19 | | /// </summary> |
| | 10165 | 20 | | public SerializationTypeOptions() |
| | | 21 | | { |
| | 10165 | 22 | | AliasTypeDictionary = new ReadOnlyDictionary<string, Type>(_aliasTypeDictionary); |
| | 10165 | 23 | | TypeAliasDictionary = new ReadOnlyDictionary<Type, string>(_typeAliasDictionary); |
| | | 24 | | |
| | 10165 | 25 | | RegisterTypeAlias(typeof(short), "Int16"); |
| | 10165 | 26 | | RegisterTypeAlias(typeof(int), "Int32"); |
| | 10165 | 27 | | RegisterTypeAlias(typeof(long), "Int64"); |
| | 10165 | 28 | | RegisterLegacyTypeName(typeof(long), "Long"); |
| | 10165 | 29 | | RegisterTypeAlias(typeof(float), "Single"); |
| | 10165 | 30 | | RegisterTypeAlias(typeof(object), "Object"); |
| | 10165 | 31 | | RegisterTypeAlias(typeof(string), "String"); |
| | 10165 | 32 | | RegisterTypeAlias(typeof(bool), "Boolean"); |
| | 10165 | 33 | | RegisterTypeAlias(typeof(decimal), "Decimal"); |
| | 10165 | 34 | | RegisterTypeAlias(typeof(double), "Double"); |
| | 10165 | 35 | | RegisterTypeAlias(typeof(byte[]), "ByteArray"); |
| | 10165 | 36 | | RegisterTypeAlias(typeof(Guid), nameof(Guid)); |
| | 10165 | 37 | | RegisterTypeAlias(typeof(DateTime), nameof(DateTime)); |
| | 10165 | 38 | | RegisterTypeAlias(typeof(DateTimeOffset), nameof(DateTimeOffset)); |
| | 10165 | 39 | | RegisterTypeAlias(typeof(TimeSpan), nameof(TimeSpan)); |
| | 10165 | 40 | | RegisterTypeAlias(typeof(Stream), nameof(Stream)); |
| | 10165 | 41 | | RegisterTypeAlias(typeof(ExpandoObject), "JSON"); |
| | 10165 | 42 | | RegisterTypeAlias(typeof(JsonElement), nameof(JsonElement)); |
| | 10165 | 43 | | RegisterTypeAlias(typeof(JsonNode), nameof(JsonNode)); |
| | 10165 | 44 | | RegisterTypeAlias(typeof(JsonObject), nameof(JsonObject)); |
| | 10165 | 45 | | RegisterTypeAlias(typeof(JsonArray), nameof(JsonArray)); |
| | 10165 | 46 | | RegisterTypeAlias(typeof(IDictionary<string, string>), "StringDictionary"); |
| | 10165 | 47 | | RegisterTypeAlias(typeof(IDictionary<string, object>), "ObjectDictionary"); |
| | 10165 | 48 | | RegisterTypeAlias(typeof(Dictionary<string, string>), "StringMap"); |
| | 10165 | 49 | | RegisterTypeAlias(typeof(Dictionary<string, object>), "ObjectMap"); |
| | 10165 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets aliases and legacy names keyed by identifier. |
| | | 54 | | /// </summary> |
| | 10165 | 55 | | public IDictionary<string, Type> AliasTypeDictionary { get; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets preferred aliases keyed by type. |
| | | 59 | | /// </summary> |
| | 10165 | 60 | | public IDictionary<Type, string> TypeAliasDictionary { get; } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Registers a preferred serialization alias. |
| | | 64 | | /// </summary> |
| | | 65 | | public SerializationTypeOptions RegisterTypeAlias(Type type, string alias) |
| | | 66 | | { |
| | 244341 | 67 | | _aliasTypeDictionary[alias] = type; |
| | 244341 | 68 | | _typeAliasDictionary[type] = alias; |
| | 244341 | 69 | | return this; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Registers a legacy serialization identifier for compatibility reads. |
| | | 74 | | /// </summary> |
| | | 75 | | public SerializationTypeOptions RegisterLegacyTypeName(Type type, string typeName) |
| | | 76 | | { |
| | 10189 | 77 | | _aliasTypeDictionary[typeName] = type; |
| | 10189 | 78 | | return this; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Registers the type's simple assembly-qualified name as a legacy serialization identifier. |
| | | 83 | | /// </summary> |
| | 21 | 84 | | public SerializationTypeOptions RegisterLegacySimpleAssemblyQualifiedName(Type type) => RegisterLegacyTypeName(type, |
| | | 85 | | } |