/*********************************************************************** * 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 { /// /// Json文件读写 /// 引用Newtonsoft.Json /// 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(); } /// /// 读取Json返回实体对象 /// /// public T Read() => Read(""); /// /// 根据节点读取Json返回实体对象 /// /// public T Read(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(secJt.ToString()); } } else { return JsonConvert.DeserializeObject(jObj.ToString()); } return default; } /// /// 读取Json返回集合 /// /// public List ReadList() => ReadList(""); /// /// 根据节点读取Json返回集合 /// /// public List ReadList(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>(secJt.ToString()); } } else { return JsonConvert.DeserializeObject>(jObj.ToString()); } return default; } /// /// 写入文件 /// /// 自定义对象 /// public void Write(T t) => Write("", t); /// /// 写入指定section文件 /// /// 自定义对象 /// public void Write(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); } /// /// 删除指定section节点 /// /// /// 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); } } } }