using System.Text.Json; using GhostImageManager.Models; namespace GhostImageManager.Services; public class GhostConfigService { private readonly string _configPath; private readonly object _lock = new(); public GhostConfigService(IWebHostEnvironment env) { _configPath = Path.Combine(env.ContentRootPath, "ghost-instances.json"); } public GhostConfig Load() { if (!File.Exists(_configPath)) { var empty = new GhostConfig(); Save(empty); return empty; } var json = File.ReadAllText(_configPath); return JsonSerializer.Deserialize(json, GetOptions()) ?? new GhostConfig(); } public void Save(GhostConfig config) { lock (_lock) { var json = JsonSerializer.Serialize(config, GetOptions()); File.WriteAllText(_configPath, json); } } private static JsonSerializerOptions GetOptions() => new() { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; }