| | | 1 | | using System.Text.Encodings.Web; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | using System.Text.Json.Serialization.Metadata; |
| | | 5 | | using System.Text.Unicode; |
| | | 6 | | using Elsa.Common.Converters; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Common.Serialization; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// A base class for configurable JSON serializers. |
| | | 13 | | /// </summary> |
| | | 14 | | public abstract class ConfigurableSerializer |
| | | 15 | | { |
| | | 16 | | private JsonSerializerOptions? _options; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="ConfigurableSerializer"/> class. |
| | | 20 | | /// </summary> |
| | 6 | 21 | | protected ConfigurableSerializer(IServiceProvider serviceProvider) |
| | | 22 | | { |
| | 6 | 23 | | ServiceProvider = serviceProvider; |
| | 6 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the service provider. |
| | | 28 | | /// </summary> |
| | 3501 | 29 | | protected IServiceProvider ServiceProvider { get; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Creates a new instance of <see cref="JsonSerializerOptions"/> with the configured options. |
| | | 33 | | /// </summary> |
| | | 34 | | public virtual JsonSerializerOptions GetOptions() |
| | | 35 | | { |
| | 3130 | 36 | | if (_options != null) |
| | 3124 | 37 | | return _options; |
| | | 38 | | |
| | 6 | 39 | | var options = CreateOptionsInternal(); |
| | 6 | 40 | | ApplyOptions(options); |
| | 6 | 41 | | _options = options; |
| | 6 | 42 | | return options; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Creates a new instance of <see cref="JsonSerializerOptions"/> with the configured options. |
| | | 47 | | /// </summary> |
| | | 48 | | public virtual void ApplyOptions(JsonSerializerOptions options) |
| | | 49 | | { |
| | 588 | 50 | | Configure(options); |
| | 588 | 51 | | AddConverters(options); |
| | 588 | 52 | | RunConfigurators(options); |
| | 588 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Creates a new instance of <see cref="JsonSerializerOptions"/>. |
| | | 57 | | /// </summary> |
| | | 58 | | protected JsonSerializerOptions GetOptionsInternal() |
| | | 59 | | { |
| | 582 | 60 | | var options = CreateOptionsInternal(); |
| | 582 | 61 | | ApplyOptions(options); |
| | 582 | 62 | | _options = options; |
| | 582 | 63 | | return options; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Creates a new instance of <see cref="JsonSerializerOptions"/>. |
| | | 68 | | /// </summary> |
| | | 69 | | private static JsonSerializerOptions CreateOptionsInternal() |
| | | 70 | | { |
| | 588 | 71 | | var options = new JsonSerializerOptions |
| | 588 | 72 | | { |
| | 588 | 73 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 588 | 74 | | PropertyNameCaseInsensitive = true, |
| | 588 | 75 | | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| | 588 | 76 | | Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) |
| | 588 | 77 | | }; |
| | | 78 | | |
| | 588 | 79 | | options.Converters.Add(new JsonStringEnumConverter()); |
| | 588 | 80 | | options.Converters.Add(JsonMetadataServices.TimeSpanConverter); |
| | 588 | 81 | | options.Converters.Add(new IntegerJsonConverter()); |
| | 588 | 82 | | options.Converters.Add(new BigIntegerJsonConverter()); |
| | 588 | 83 | | options.Converters.Add(new DecimalJsonConverter()); |
| | | 84 | | |
| | 588 | 85 | | return options; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Configures the <see cref="JsonSerializerOptions"/> object. |
| | | 90 | | /// </summary> |
| | | 91 | | protected virtual void Configure(JsonSerializerOptions options) |
| | | 92 | | { |
| | 587 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Adds additional <see cref="JsonConverter"/> objects. |
| | | 97 | | /// </summary> |
| | | 98 | | protected virtual void AddConverters(JsonSerializerOptions options) |
| | | 99 | | { |
| | 3 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Give external packages a chance to further configure the serializer options. |
| | | 104 | | /// </summary> |
| | | 105 | | protected virtual void RunConfigurators(JsonSerializerOptions options) |
| | | 106 | | { |
| | 588 | 107 | | var configurators = ServiceProvider.GetServices<ISerializationOptionsConfigurator>(); |
| | 588 | 108 | | var modifiers = new List<Action<JsonTypeInfo>>(); |
| | | 109 | | |
| | 8232 | 110 | | foreach (var configurator in configurators) |
| | | 111 | | { |
| | 3528 | 112 | | configurator.Configure(options); |
| | 3528 | 113 | | var modifiersToAdd = configurator.GetModifiers(); |
| | 3528 | 114 | | modifiers.AddRange(modifiersToAdd); |
| | | 115 | | } |
| | | 116 | | |
| | 588 | 117 | | options.TypeInfoResolver = new ModifiableJsonTypeInfoResolver(modifiers); |
| | 588 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Creates an instance of the specified type using the service provider. |
| | | 122 | | /// </summary> |
| | 2912 | 123 | | protected T CreateInstance<T>() => ActivatorUtilities.CreateInstance<T>(ServiceProvider); |
| | | 124 | | } |