You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.0 KiB
38 lines
1.0 KiB
1 year ago
|
using System.Collections.Generic;
|
||
|
using System.Text.Json.Nodes;
|
||
|
|
||
|
namespace StabilityMatrix.Avalonia.Models.Inference;
|
||
|
|
||
|
public class ComponentDictionary<T> : Dictionary<string, T>, IJsonLoadableState where T : IJsonLoadableState
|
||
|
{
|
||
|
/// <inheritdoc />
|
||
|
public void LoadStateFromJsonObject(JsonObject state)
|
||
|
{
|
||
|
// For each existing key, load the state from the json object
|
||
|
foreach (var (key, value) in state)
|
||
|
{
|
||
|
if (value is null) continue;
|
||
|
|
||
|
if (TryGetValue(key, out var existingValue))
|
||
|
{
|
||
|
existingValue.LoadStateFromJsonObject(value.AsObject());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <inheritdoc />
|
||
|
public JsonObject SaveStateToJsonObject()
|
||
|
{
|
||
|
// Create a new json object
|
||
|
var state = new JsonObject();
|
||
|
|
||
|
// For each existing key, save the state to the json object
|
||
|
foreach (var (key, value) in this)
|
||
|
{
|
||
|
state.Add(key, value.SaveStateToJsonObject());
|
||
|
}
|
||
|
|
||
|
return state;
|
||
|
}
|
||
|
}
|