/ src / settings-ui / Settings.UI.Library / CustomAction / SendCustomAction.cs
SendCustomAction.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System.Collections.Concurrent;
 6  using System.Text.Json;
 7  using System.Text.Json.Serialization;
 8  
 9  namespace Microsoft.PowerToys.Settings.UI.Library.CustomAction
10  {
11      public class SendCustomAction
12      {
13          private static readonly ConcurrentDictionary<string, JsonSerializerOptions> OptionsCache = new ConcurrentDictionary<string, JsonSerializerOptions>();
14  
15          private readonly string moduleName;
16  
17          public SendCustomAction(string moduleName)
18          {
19              this.moduleName = moduleName;
20          }
21  
22          [JsonPropertyName("action")]
23          public ModuleCustomAction Action { get; set; }
24  
25          public string ToJsonString()
26          {
27              var jsonSerializerOptions = OptionsCache.GetOrAdd(moduleName, CreateOptionsForModuleName);
28              return JsonSerializer.Serialize(this, jsonSerializerOptions);
29          }
30  
31          private JsonSerializerOptions CreateOptionsForModuleName(string moduleName)
32          {
33              return new JsonSerializerOptions
34              {
35                  PropertyNamingPolicy = new CustomNamePolicy((propertyName) =>
36                  {
37                      // Using Ordinal as this is an internal property name
38                      return propertyName.Equals("ModuleAction", System.StringComparison.Ordinal) ? moduleName : propertyName;
39                  }),
40              };
41          }
42      }
43  }