终端一体化运控平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

FJson.cs 2.0 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace BPASmartClient.Compiler
  8. {
  9. public class FJson<T> where T : class, new()
  10. {
  11. private static string path
  12. {
  13. get
  14. {
  15. Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"AccessFile\\JSON"));
  16. return AppDomain.CurrentDomain.BaseDirectory + "AccessFile\\JSON\\" + typeof(T).Name + ".json";
  17. }
  18. }
  19. public static T Data
  20. {
  21. get;
  22. set;
  23. } = new T();
  24. public static void Save()
  25. {
  26. string contents = JsonConvert.SerializeObject(Data);
  27. File.WriteAllText(path,contents);
  28. }
  29. public static void Read()
  30. {
  31. if (File.Exists(path))
  32. {
  33. T val = JsonConvert.DeserializeObject<T>(File.ReadAllText(path));
  34. if (val != null)
  35. {
  36. Data = val;
  37. }
  38. }
  39. }
  40. public static void SaveInterface()
  41. {
  42. JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
  43. jsonSerializerSettings.TypeNameHandling = TypeNameHandling.Objects;
  44. string contents = JsonConvert.SerializeObject(Data,Formatting.Indented,jsonSerializerSettings);
  45. File.WriteAllText(path,contents);
  46. }
  47. public static void ReadInterface()
  48. {
  49. if (File.Exists(path))
  50. {
  51. JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
  52. jsonSerializerSettings.TypeNameHandling = TypeNameHandling.Objects;
  53. T val = JsonConvert.DeserializeObject<T>(File.ReadAllText(path),jsonSerializerSettings);
  54. if (val != null)
  55. {
  56. Data = val;
  57. }
  58. }
  59. }
  60. }
  61. }