-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppSettingsUpdater.cs
87 lines (76 loc) · 3.5 KB
/
AppSettingsUpdater.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
/// <summary>
/// Since <see cref="IConfiguration"/> cannot write to appSettings.json,
/// this class handles the ability to re-generae the structure.
/// Credit for idea: https://stackoverflow.com/a/67917167
/// </summary>
/// <example><![CDATA[
/// var settingsUpdater = new AppSettingsUpdater("{}");
/// settingsUpdater.UpdateAppSetting("OuterProperty:NestedProperty:PropertyToUpdate", "new value");
/// // result:
/// // { "OuterProperty": { "NestedProperty": { "PropertyToUpdate": "new value" } } }
/// ]]></example>
public class AppSettingsUpdater
{
private const string EmptyJson = "{}";
private static readonly JsonSerializerOptions SerializerOptions = new();
static AppSettingsUpdater()
{
// the new serializer is a bit more aggressive and serializes some ASCII as \uxxxx
TextEncoderSettings encoderSettings = new();
encoderSettings.AllowCharacters('\u002B'); // + == \u002B
encoderSettings.AllowRange(UnicodeRanges.BasicLatin);
SerializerOptions.WriteIndented = true;
SerializerOptions.Encoder = JavaScriptEncoder.Create(encoderSettings);
}
/// <summary>Creates an updater using the JSON content of an appSettings file</summary>
/// <param name="jsonContent">The JSON content of an appSettings.json file.</param>
public AppSettingsUpdater(string jsonContent)
{
Content = jsonContent;
}
/// <summary>The current content, after <see cref="UpdateAppSetting"/> has been called.</summary>
public string Content { get; private set; }
public void UpdateAppSetting(string key, object? value)
{
// Empty keys "" are allowed in json by the way
if (key == null)
{
throw new ArgumentException("Json property key cannot be null", nameof(key));
}
var updatedConfigDict = UpdateJson(key, value, Content);
// After receiving the dictionary with updated key value pair, we serialize it back into json.
var updatedJson = JsonSerializer.Serialize(updatedConfigDict, SerializerOptions);
Content = updatedJson;
}
/// <summary>
/// This method will recursively read json segments separated by semicolon (firstObject:nestedObject:someProperty)
/// until it reaches the desired property that needs to be updated,
/// it will update the property and return json document represented by dictonary of dictionaries of dictionaries and so on.
/// This dictionary structure can be easily serialized back into json
/// </summary>
private IDictionary<string, object?> UpdateJson(string key, object? value, string jsonSegment)
{
const char keySeparator = ':';
var config = JsonSerializer.Deserialize<SortedDictionary<string, object?>>(jsonSegment)!;
var keyParts = key.Split(keySeparator);
var isKeyNested = keyParts.Length > 1;
if (isKeyNested)
{
var firstKeyPart = keyParts[0];
var remainingKey = string.Join(keySeparator, keyParts.Skip(1));
// If the key does not exist already, we will create a new key and append it to the json
var newJsonSegment = config.ContainsKey(firstKeyPart) && config[firstKeyPart] != null
? config[firstKeyPart]?.ToString()
: EmptyJson;
config[firstKeyPart] = UpdateJson(remainingKey, value, newJsonSegment!);
}
else
{
config[key] = value;
}
return config;
}
}