Compare commits

..

11 Commits

Author SHA1 Message Date
7cea589294 test: 删除配置项示例 2025-01-21 19:23:57 +08:00
ad15422700 feat: 添加删除配置项的功能 2025-01-21 19:23:47 +08:00
6e3450fef2 docs: 更新使用方法说明 2025-01-21 19:15:49 +08:00
47e80bfebc refactor: 清理代码 2025-01-21 19:15:35 +08:00
17f78685fc feat: 添加 Get 方法的重载版本 2025-01-21 19:12:04 +08:00
bfe24d1e4b test: 更新配置测试用例 2025-01-21 19:02:09 +08:00
5cd01a6375 feat: 优化配置文件读写逻辑
- 修改配置数据初始化方式,提高代码可读性- 完善 Get 方法,增加默认值设置和类型转换逻辑- 重构 Set 方法,优化配置数据更新流程
- 更新解决方案设置
2025-01-21 19:01:55 +08:00
a11b29a21d refactor: 重构配置文件读取逻辑 2025-01-21 17:59:33 +08:00
2aeef0227c feat: 添加测试项目并更新解决方案配置 2025-01-21 17:47:23 +08:00
6b2c87c031 feat: 添加配置文件管理功能 2025-01-21 17:47:06 +08:00
539a0cb4a3 build: 添加 Newtonsoft.Json 依赖 2025-01-21 17:46:45 +08:00
7 changed files with 243 additions and 1 deletions

View File

@@ -8,6 +8,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Folder", "Solution
Readme.MD = Readme.MD
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -18,5 +20,9 @@ Global
{96DD0E6E-8CE2-42DE-87A9-111D9198CE76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96DD0E6E-8CE2-42DE-87A9-111D9198CE76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96DD0E6E-8CE2-42DE-87A9-111D9198CE76}.Release|Any CPU.Build.0 = Release|Any CPU
{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0FF027F-CAFB-4DD2-8B59-4CFD9DAFBB04}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACastHelpers_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F6d2342c247c64eb7a9b3702e481e4443f83e00_003F54_003Ff2c78e92_003FCastHelpers_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AExceptionDispatchInfo_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fbf9021a960b74107a7e141aa06bc9d8a0a53c929178c2fb95b1597be8af8dc_003FExceptionDispatchInfo_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFileInfo_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F2610c78ec589e1aacd82c4428835c427b838e8156a6f346f517d28ef9ffbb9a2_003FFileInfo_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFile_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F6d2342c247c64eb7a9b3702e481e4443f83e00_003Fa1_003F11a17405_003FFile_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AJsonSerializerInternalReader_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F7e62198beab24380bbac29171862d1d8adf10_003F90_003F0d01fe57_003FJsonSerializerInternalReader_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AJsonSerializerInternalReader_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F29ea32a76b2a6b8eb246f247df6a6a86d887384b4c31db2a35effccdd86c24_003FJsonSerializerInternalReader_002Ecs_002Fz_003A2_002D1/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>

135
JackCraft.Config/Config.cs Normal file
View File

@@ -0,0 +1,135 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace JackCraft.Config;
public class Config
{
private readonly FileInfo _configFile;
public readonly string ConfigFileName;
public readonly string ConfigFilePath;
private Dictionary<string, object> _configData = new();
public Config(FileInfo configFile)
{
_configFile = configFile;
ConfigFilePath = configFile.DirectoryName ?? string.Empty;
ConfigFileName = configFile.Name;
if (!configFile.Exists)
{
configFile.Directory?.Create();
using var stream = configFile.Create();
File.WriteAllText(configFile.FullName, "{}");
}
LoadConfigData();
}
private void LoadConfigData()
{
if (File.Exists(_configFile.FullName))
{
var existingJson = File.ReadAllText(_configFile.FullName);
_configData = JsonConvert.DeserializeObject<Dictionary<string, object>>(existingJson) ??
new Dictionary<string, object>();
}
else
{
_configData = new Dictionary<string, object>();
}
}
public T? Get<T>(string key)
{
return Get<T>(key, default);
}
public T? Get<T>(string key, T? defaultValue)
{
LoadConfigData();
var keys = key.Split('.');
var currentData = _configData;
for (var i = 0; i < keys.Length - 1; i++)
{
if (!currentData.TryGetValue(keys[i], out var tValue))
{
Set(key, defaultValue);
return defaultValue;
}
switch (tValue)
{
case JObject jObject:
currentData = jObject.ToObject<Dictionary<string, object>>() ?? new Dictionary<string, object>();
break;
case Dictionary<string, object> nestedDict:
currentData = nestedDict;
break;
default:
Set(key, defaultValue);
return defaultValue;
}
}
if (currentData.TryGetValue(keys[^1], out var result) && result is T typedResult) return typedResult;
Set(key, defaultValue);
return defaultValue;
}
public void Set<T>(string key, T? value)
{
LoadConfigData();
var keys = key.Split('.');
var currentData = _configData;
for (var i = 0; i < keys.Length - 1; i++)
{
var currentKey = keys[i];
if (!currentData.TryGetValue(currentKey, out var tValue))
{
tValue = new Dictionary<string, object>();
currentData[currentKey] = tValue;
}
if (tValue is JObject jObject)
currentData[currentKey] =
jObject.ToObject<Dictionary<string, object>>() ?? new Dictionary<string, object>();
currentData = (Dictionary<string, object>)currentData[currentKey];
}
var finalKey = keys[^1];
currentData[finalKey] = value!;
var json = JsonConvert.SerializeObject(_configData, Formatting.Indented);
File.WriteAllText(_configFile.FullName, json);
}
public void Delete(string key)
{
LoadConfigData();
var keys = key.Split('.');
var currentData = _configData;
for (var i = 0; i < keys.Length - 1; i++)
{
var currentKey = keys[i];
if (!currentData.TryGetValue(currentKey, out var value)) return;
if (value is JObject jObject)
currentData[currentKey] =
jObject.ToObject<Dictionary<string, object>>() ?? new Dictionary<string, object>();
currentData = (Dictionary<string, object>)currentData[currentKey];
}
var finalKey = keys[^1];
currentData.Remove(finalKey);
var json = JsonConvert.SerializeObject(_configData, Formatting.Indented);
File.WriteAllText(_configFile.FullName, json);
}
}

View File

@@ -7,4 +7,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
</ItemGroup>
</Project>

View File

@@ -1,3 +1,49 @@
# JackCraft.Config
一个自己觉得怎么好用怎么写的配置文件库
## 使用方法
### 获取配置文件
> 配置文件不存在时会自动创建一个空 Json 文件,但是若配置文件不是 Json 标准格式将会在写入和读取键值时抛出异常
```csharp
// 标准示例: var config = new Config(new FileInfo(配置文件路径));
var config = new Config(new FileInfo("./config.json"));
```
### 读取键值
> 若键不存在则返回默认值,并且同时也会写入键值(若没有提供默认值则写入空)
```csharp
// 标准示例: var value = config.Get<T>(键, <可选>默认值);
// {
// "114514": "1919810"
// }
var value = config.Get("114514", "1919810");
var value = config.Get<string>("114514");
// {
// "abc": {
// "def": 1234
// }
// }
var value = config.Get("abc.def", 1234);
var value = config.Get<int>("abc.def");
```
### 写入键值
```csharp
// 标准示例: config.Set<T>(键, 值);
// {
// "114514": "1919810"
// }
config.Set("114514", "1919810");
// 其他类型与 Get<T> 同理...
```

29
Test/Program.cs Normal file
View File

@@ -0,0 +1,29 @@
using JackCraft.Config;
namespace Test;
internal static class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
var testClass = new TestClass
{
Test = "test",
Abc = 123
};
var config = new Config(new FileInfo("./config.json"));
var a = config.Get("aaa.www.www", "555");
var b = config.Get("aaa.bbb", "1");
var c = config.Get("aaa.sss", "1");
var d = config.Get("bbb.sss", "1");
config.Delete("aaa.www");
}
}
public class TestClass
{
public string? Test { get; set; }
public int Abc { get; set; }
}

15
Test/Test.csproj Normal file
View File

@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<LangVersion>latestmajor</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\JackCraft.Config\JackCraft.Config.csproj"/>
</ItemGroup>
</Project>