-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
Copy pathJsonConfiguration.cs
103 lines (85 loc) · 3.55 KB
/
JsonConfiguration.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Text;
using System.Xml;
using pyRevitLabs.Configurations.Abstractions;
using pyRevitLabs.Configurations.Exceptions;
using pyRevitLabs.Json;
using pyRevitLabs.Json.Linq;
using Formatting = pyRevitLabs.Json.Formatting;
namespace pyRevitLabs.Configurations.Json;
public sealed class JsonConfiguration : ConfigurationBase
{
public static readonly Encoding DefaultJsonFileEncoding = Encoding.UTF8;
private readonly JObject _jsonObject;
/// <summary>
/// Create json configuration instance.
/// </summary>
/// <param name="configurationPath">Configuration path.</param>
/// <param name="readOnly">Readonly configurations </param>
private JsonConfiguration(string configurationPath, bool readOnly)
: base(configurationPath, readOnly)
{
_jsonObject = !File.Exists(configurationPath)
? new JObject()
: JObject.Parse(File.ReadAllText(_configurationPath, DefaultJsonFileEncoding));
}
/// <summary>
/// Creates JsonConfiguration.
/// </summary>
/// <param name="configurationPath">Configuration file path.</param>
/// <param name="readOnly">Mark file is readonly.</param>
/// <returns>Return new JsonConfiguration.</returns>
/// <exception cref="ArgumentNullException">When configurationPath is null.</exception>
public static IConfiguration Create(string configurationPath, bool readOnly = default)
{
if (configurationPath is null)
throw new ArgumentNullException(nameof(configurationPath));
return new JsonConfiguration(configurationPath, readOnly);
}
protected override void SaveConfigurationImpl()
{
string jsonString = JsonConvert.SerializeObject(_jsonObject,
new JsonSerializerSettings() {Formatting = Formatting.Indented});
File.WriteAllText(_configurationPath, jsonString, DefaultJsonFileEncoding);
}
protected override bool HasSectionImpl(string sectionName)
{
return _jsonObject.ContainsKey(sectionName);
}
protected override bool HasSectionKeyImpl(string sectionName, string keyName)
{
JObject? sectionObject = _jsonObject[sectionName] as JObject;
return sectionObject?.ContainsKey(keyName) == true;
}
protected override bool RemoveValueImpl(string sectionName, string keyName)
{
_jsonObject[sectionName]![keyName] = null;
return true;
}
protected override T GetValueImpl<T>(string sectionName, string keyName)
{
JToken? token = _jsonObject[sectionName]?[keyName];
return token is null
? throw new ConfigurationException($"Section {sectionName} or keyName {keyName} not found.")
: token.ToObject<T>()
?? throw new ConfigurationException($"Cannot deserialize value with {sectionName} and {keyName}.");
}
protected override void SetValueImpl<T>(string sectionName, string keyName, T value)
{
if (!HasSection(sectionName))
{
JObject fromObject = new();
fromObject.Add(keyName, JToken.FromObject(value!));
_jsonObject.Add(sectionName, fromObject);
return;
}
if (!HasSectionKey(sectionName, keyName))
{
JObject fromObject = new();
fromObject.Add(keyName, JToken.FromObject(value!));
JObject? sectionObject = (JObject?)_jsonObject[sectionName];
sectionObject?.Add(keyName, fromObject);
return;
}
_jsonObject[sectionName]![keyName] = JToken.FromObject(value!);
}
}