| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Text.Json;
- using System.Text.Json.Serialization.Metadata;
- namespace crusherScanner
- {
- internal class JsonHandler
- {
- private string filePath;
- private bool isSettingsFile;
- public Settings settings;
- public List<Logs>? log;
- /// <summary>
- /// Open or create a JSON file.
- /// </summary>
- /// <param name="filePath">Path to where the file is located or should be created.</param>
- /// <param name="isSettingsfile">Is this a settings file? or a log file.</param>
- public JsonHandler(string filePath,bool isSettingsfile)
- {
- this.filePath = filePath;
- this.isSettingsFile = isSettingsfile;
- if (isSettingsfile)
- {
- if (File.Exists(filePath))
- {
- string fileData = File.ReadAllText(filePath);
- settings = JsonSerializer.Deserialize<Settings>(fileData);
- }
- else
- {
- settings = new Settings();
- }
- }
- else
- {
- if (File.Exists(filePath))
- {
- string fileData = File.ReadAllText(filePath);
- log = JsonSerializer.Deserialize<List<Logs>>(fileData);
- }
- else
- {
- log = new List<Logs>();
- }
- }
- }
- public void Save()
- {
- string fileData;
- if (isSettingsFile)
- {
- fileData = JsonSerializer.Serialize(settings);
- }
- else
- {
- fileData = JsonSerializer.Serialize(log);
- }
- File.WriteAllText(filePath, fileData);
- }
- }
- }
|