移动系统liao
2024-08-15 f43970a061d3c90520b4e8f48caa17a204d9a085
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/***********************************************************************
 *            Project: baifenBinfa.Net                                     *
 *                Web: https://baifenBinfa.com                             *
 *        ProjectName: 百分兵法管理系统                               *
 *             Author:                                        *
 *              Email:                               *
 *         CreateTime: 2020-03-02 23:15:19
 *        Description: 暂无
 ***********************************************************************/
 
 
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 
namespace CoreCms.Net.Utility.Helper
{
    /// <summary>
    /// Json文件读写
    /// 引用Newtonsoft.Json
    /// </summary>
    public class JsonFileHelper
    {
        //注意:section为根节点
        private readonly string _path;
        private IConfiguration Configuration { get; set; }
        public JsonFileHelper(string jsonName)
        {
            _path = !jsonName.EndsWith(".json") ? $"{jsonName}.json" : jsonName;
            //ReloadOnChange = true 当*.json文件被修改时重新加载            
            Configuration = new ConfigurationBuilder()
            .Add(new JsonConfigurationSource { Path = _path, ReloadOnChange = true, Optional = true })
            .Build();
        }
 
        /// <summary>
        /// 读取Json返回实体对象
        /// </summary>
        /// <returns></returns>
        public T Read<T>() => Read<T>("");
 
        /// <summary>
        /// 根据节点读取Json返回实体对象
        /// </summary>
        /// <returns></returns>
        public T Read<T>(string section)
        {
            using var file = new StreamReader(_path);
            using var reader = new JsonTextReader(file);
            var jObj = (JObject)JToken.ReadFrom(reader);
            if (!string.IsNullOrWhiteSpace(section))
            {
                var secJt = jObj[section];
                if (secJt != null)
                {
                    return JsonConvert.DeserializeObject<T>(secJt.ToString());
                }
            }
            else
            {
                return JsonConvert.DeserializeObject<T>(jObj.ToString());
            }
 
            return default;
        }
 
        /// <summary>
        /// 读取Json返回集合
        /// </summary>
        /// <returns></returns>
        public List<T> ReadList<T>() => ReadList<T>("");
 
        /// <summary>
        /// 根据节点读取Json返回集合
        /// </summary>
        /// <returns></returns>
        public List<T> ReadList<T>(string section)
        {
            using var file = new StreamReader(_path);
            using var reader = new JsonTextReader(file);
            var jObj = (JObject)JToken.ReadFrom(reader);
            if (!string.IsNullOrWhiteSpace(section))
            {
                var secJt = jObj[section];
                if (secJt != null)
                {
                    return JsonConvert.DeserializeObject<List<T>>(secJt.ToString());
                }
            }
            else
            {
                return JsonConvert.DeserializeObject<List<T>>(jObj.ToString());
            }
 
            return default;
        }
 
        /// <summary>
        /// 写入文件
        /// </summary>
        /// <typeparam name="T">自定义对象</typeparam>
        /// <param name="t"></param>
        public void Write<T>(T t) => Write("", t);
 
        /// <summary>
        /// 写入指定section文件
        /// </summary>
        /// <typeparam name="T">自定义对象</typeparam>
        /// <param name="t"></param>
        public void Write<T>(string section, T t)
        {
            JObject jObj;
            using (StreamReader file = new StreamReader(_path))
            using (JsonTextReader reader = new JsonTextReader(file))
            {
                jObj = (JObject)JToken.ReadFrom(reader);
                var json = JsonConvert.SerializeObject(t);
                if (string.IsNullOrWhiteSpace(section))
                    jObj = JObject.Parse(json);
                else
                    jObj[section] = JObject.Parse(json);
            }
 
            using var writer = new StreamWriter(_path);
            using var jsonWriter = new JsonTextWriter(writer);
            jObj.WriteTo(jsonWriter);
        }
 
        /// <summary>
        /// 删除指定section节点
        /// </summary>
        /// <param name="section"></param>
        /// <exception cref="Exception"></exception>
        public void Remove(string section)
        {
            JObject jObj;
            using (StreamReader file = new StreamReader(_path))
            using (JsonTextReader reader = new JsonTextReader(file))
            {
                jObj = (JObject)JToken.ReadFrom(reader);
                jObj.Remove(section);
            }
 
            using (var writer = new StreamWriter(_path))
            using (var jsonWriter = new JsonTextWriter(writer))
            {
                jObj.WriteTo(jsonWriter);
            }
        }
    }
}