Skip to content

Commit f70f3ea

Browse files
committed
feat: config files are TOML instead of JSON
1 parent a8b5cce commit f70f3ea

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

ThirdPartyLicenses/LICENSE-Tomlyn

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Copyright (c) 2019-2022, Alexandre Mutel
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification
5+
, are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
2. Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using System.Collections.Generic;
2-
using System.Text.Json.Serialization;
32

43
namespace SharpWebserver.Config;
54

65
public class ClientBlockList
76
{
8-
[JsonInclude]
9-
public List<string> BlockList = [];
7+
public List<string> BlockList { get; set; } = [];
108
}

src/SharpWebserver/Config/ConfigManager.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
using System.IO;
2-
using System.Text.Json;
2+
using Tomlyn;
33

44
namespace SharpWebserver;
55

66
public static class ConfigManager
77
{
8-
public static T? LoadConfig<T>(string fileName) where T : class
8+
public static readonly TomlModelOptions Options = new()
9+
{
10+
ConvertPropertyName = x => x,
11+
};
12+
13+
public static T? LoadConfig<T>(string fileName) where T : class, new()
914
{
1015
var path = Path.Combine(ListenServer.ConfigDir, fileName);
1116
var config = new FileInfo(path);
@@ -19,18 +24,23 @@ public static class ConfigManager
1924
using (var reader = config.OpenText())
2025
{
2126
string objectData = reader.ReadToEnd();
22-
var thing = JsonSerializer.Deserialize<T>(objectData);
2327

24-
if (thing is not null)
28+
try
2529
{
26-
Logger.LogTrace("Finished loading config from disk");
27-
return thing;
30+
var thing = Toml.ToModel<T>(objectData, options: Options);
31+
if (thing is not null)
32+
{
33+
Logger.LogTrace("Finished loading config from disk");
34+
return thing;
35+
}
36+
}
37+
catch
38+
{
39+
Logger.LogError("Unable to parse data as generic type T", [
40+
("Size", objectData.Length),
41+
("Type", typeof(T))
42+
]);
2843
}
29-
30-
Logger.LogError("Unable to parse data as generic type T", [
31-
("Size", objectData.Length),
32-
("Type", typeof(T))
33-
]);
3444
}
3545
}
3646

@@ -42,9 +52,9 @@ public static void SaveConfig(string fileName, object configObject)
4252
var path = Path.Combine(ListenServer.ConfigDir, fileName);
4353
var configFile = new FileInfo(path);
4454

45-
using (var writer = configFile.OpenWrite())
55+
using (var writer = new StreamWriter(new FileStream(configFile.FullName, FileMode.Truncate)))
4656
{
47-
JsonSerializer.Serialize(writer, configObject);
57+
writer.Write(Toml.FromModel(configObject, options: Options));
4858
Logger.LogInfo($"Wrote out {fileName}");
4959
}
5060
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
using System.Text.Json.Serialization;
2-
31
namespace SharpWebserver.Config;
42

53
public class SharpConfig
64
{
7-
[JsonInclude]
8-
public bool SafeMode;
9-
[JsonInclude]
10-
public int PortNumber = 80;
11-
5+
public bool SafeMode { get; set; } = false;
6+
public int PortNumber { get; set; } = 80;
127
}

src/SharpWebserver/SharpWebserver.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
<ItemGroup>
2121
<PackageReference Include="CS-Script" Version="4.8.27" />
22+
<PackageReference Include="Tomlyn" Version="0.18.0" />
2223
</ItemGroup>
2324

2425
</Project>

0 commit comments

Comments
 (0)