Browse Source

增加Json格式的配置文件工具

yuzhengyang 5 years ago
parent
commit
c257fb1672

+ 2 - 0
Azylee.Utils/Azylee.Jsons/Azylee.Jsons.csproj

@@ -46,6 +46,7 @@
   <ItemGroup>
     <Compile Include="ConvertJson.cs" />
     <Compile Include="Json.cs" />
+    <Compile Include="JsonConfigUtils\JsonConfig.cs" />
     <Compile Include="JsonFormat.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
@@ -58,5 +59,6 @@
   <ItemGroup>
     <None Include="packages.config" />
   </ItemGroup>
+  <ItemGroup />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 66 - 0
Azylee.Utils/Azylee.Jsons/JsonConfigUtils/JsonConfig.cs

@@ -0,0 +1,66 @@
+using Azylee.Core.IOUtils.FileUtils;
+using Azylee.Core.IOUtils.TxtUtils;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Jsons.JsonConfigUtils
+{
+    /// <summary>
+    /// Json 配置管理器
+    /// </summary>
+    /// <typeparam name="T"></typeparam>
+    public class JsonConfig<T> where T : new()
+    {
+        private T Config { get; set; }
+        private string FilePath { get; set; }
+        private string FilePathBackup { get; set; }
+
+        private JsonConfig() { }
+        public JsonConfig(string filepath)
+        {
+            this.FilePath = filepath;
+            this.FilePathBackup = filepath + ".backup";
+
+            // 读取默认配置文件
+            if (File.Exists(this.FilePath))
+            {
+                this.Config = Json.File2Object<T>(this.FilePath);
+            }
+            // 读取备份的配置文件
+            if (this.Config == null)
+            {
+                if (File.Exists(this.FilePathBackup))
+                {
+                    this.Config = Json.File2Object<T>(this.FilePathBackup);
+                }
+            }
+
+            if (this.Config == null)
+            {
+                this.Config = new T();
+            }
+            Save();
+        }
+
+        public T Get()
+        {
+            return this.Config;
+        }
+        public bool Save()
+        {
+            string s = Json.Object2String(this.Config);
+            bool result = TxtTool.Create(this.FilePath, s);
+            if (TxtTool.Create(this.FilePathBackup, s))
+            {
+                if (FileTool.Copy(this.FilePathBackup, this.FilePath, true))
+                {
+                    FileTool.Delete(this.FilePathBackup);
+                }
+            }
+            return result;
+        }
+    }
+}

+ 9 - 3
Azylee.Utils/Tests/Test.Ges/Program.cs

@@ -1,4 +1,5 @@
-using System;
+using Azylee.Jsons.JsonConfigUtils;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -10,9 +11,14 @@ namespace Test.Ges
     {
         static void Main(string[] args)
         {
-            Assist<User> assist = new Assist<User>();
+            //Assist<User> assist = new Assist<User>();
+
+            //Console.Read();
+
+            JsonConfig<User> jsonConfig = new JsonConfig<User>(@"D:\tmp\test\AppConfig.json");
+            jsonConfig.Get().Age = 1;
+            jsonConfig.Save();
 
-            Console.Read();
         }
     }
 }

+ 10 - 0
Azylee.Utils/Tests/Test.Ges/Test.Ges.csproj

@@ -50,5 +50,15 @@
   <ItemGroup>
     <None Include="App.config" />
   </ItemGroup>
+  <ItemGroup>
+    <ProjectReference Include="..\..\Azylee.Core\Azylee.Core.csproj">
+      <Project>{88dc61fa-95f0-41b7-9d7d-ab0f3cbd169c}</Project>
+      <Name>Azylee.Core</Name>
+    </ProjectReference>
+    <ProjectReference Include="..\..\Azylee.Jsons\Azylee.Jsons.csproj">
+      <Project>{de3ab999-96d3-4a53-a9f2-7409138d0333}</Project>
+      <Name>Azylee.Jsons</Name>
+    </ProjectReference>
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 7 - 2
Azylee.Utils/Tests/Test.Ges/User.cs

@@ -1,4 +1,5 @@
-using System;
+using Azylee.Jsons.JsonConfigUtils;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -6,7 +7,11 @@ using System.Threading.Tasks;
 
 namespace Test.Ges
 {
-    class User
+    public class User
     {
+        public int Name { get; set; }
+        public int Sex { get; set; }
+        public int Age { get; set; }
+
     }
 }