using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BPASmartClient.Compiler { public class FJson where T : class, new() { private static string path { get { Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"AccessFile\\JSON")); return AppDomain.CurrentDomain.BaseDirectory + "AccessFile\\JSON\\" + typeof(T).Name + ".json"; } } public static T Data { get; set; } = new T(); public static void Save() { string contents = JsonConvert.SerializeObject(Data); File.WriteAllText(path,contents); } public static void Read() { if (File.Exists(path)) { T val = JsonConvert.DeserializeObject(File.ReadAllText(path)); if (val != null) { Data = val; } } } public static void SaveInterface() { JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings(); jsonSerializerSettings.TypeNameHandling = TypeNameHandling.Objects; string contents = JsonConvert.SerializeObject(Data,Formatting.Indented,jsonSerializerSettings); File.WriteAllText(path,contents); } public static void ReadInterface() { if (File.Exists(path)) { JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings(); jsonSerializerSettings.TypeNameHandling = TypeNameHandling.Objects; T val = JsonConvert.DeserializeObject(File.ReadAllText(path),jsonSerializerSettings); if (val != null) { Data = val; } } } } }