using System; using System.IO; using System.Text.Json; using Lagerprogramm.Models; namespace Lagerprogramm.Services; public class JsonDataService { private readonly string _filePath; private readonly JsonSerializerOptions _options = new() { WriteIndented = true }; public JsonDataService(string filePath) => _filePath = filePath; public AppData Load() { if (!File.Exists(_filePath)) { var defaultData = new AppData(); Save(defaultData); return defaultData; } var json = File.ReadAllText(_filePath); return JsonSerializer.Deserialize(json, _options) ?? new AppData(); } public void Save(AppData data) { var dir = Path.GetDirectoryName(_filePath); if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) Directory.CreateDirectory(dir); File.WriteAllText(_filePath, JsonSerializer.Serialize(data, _options)); } }