using FileManager; using Newtonsoft.Json.Linq; #nullable enable namespace LibationFileManager; internal class EphemeralDictionary : IJsonBackedDictionary { private JObject JsonObject { get; } public EphemeralDictionary() { JsonObject = new(); } public EphemeralDictionary(JObject dataStore) { JsonObject = dataStore; } public JObject GetJObject() => (JObject)JsonObject.DeepClone(); public bool Exists(string propertyName) => JsonObject.ContainsKey(propertyName); public string? GetString(string propertyName, string? defaultValue = null) => JsonObject[propertyName]?.Value() ?? defaultValue; public T? GetNonString(string propertyName, T? defaultValue = default) => GetObject(propertyName) is object obj ? IJsonBackedDictionary.UpCast(obj) : defaultValue; public object? GetObject(string propertyName) => JsonObject[propertyName]?.Value(); public void SetString(string propertyName, string? newValue) => JsonObject[propertyName] = newValue; public void SetNonString(string propertyName, object? newValue) => JsonObject[propertyName] = newValue is null ? null : JToken.FromObject(newValue); public bool RemoveProperty(string propertyName) => JsonObject.Remove(propertyName); public string? GetStringFromJsonPath(string jsonPath) => JsonObject.SelectToken(jsonPath)?.Value(); public bool SetWithJsonPath(string jsonPath, string propertyName, string? newValue, bool suppressLogging = false) { if (JsonObject.SelectToken(jsonPath) is JToken token && token?[propertyName] is not null) { token[propertyName] = newValue; return true; } return false; } }