Files
Libation/Source/LibationFileManager/EphemeralDictionary.cs
Michael Bucari-Tovo d67692355f Enable project-wide nullable reference types
Added DB migration for nullable types.
2026-02-05 12:43:05 -07:00

45 lines
1.6 KiB
C#

using FileManager;
using Newtonsoft.Json.Linq;
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<string>() ?? defaultValue;
public T? GetNonString<T>(string propertyName, T? defaultValue = default)
=> GetObject(propertyName) is object obj ? IJsonBackedDictionary.UpCast<T>(obj) : defaultValue;
public object? GetObject(string propertyName)
=> JsonObject[propertyName]?.Value<object>();
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<string>();
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;
}
}