using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using StabilityMatrix.Avalonia.Models;
namespace StabilityMatrix.Avalonia.ViewModels;
public abstract class LoadableViewModelBase : ViewModelBase, IJsonLoadableState
{
///
public abstract void LoadStateFromJsonObject(JsonObject state);
///
public abstract JsonObject SaveStateToJsonObject();
///
/// Serialize a model to a JSON object.
///
protected static JsonObject SerializeModel(T model)
{
var node = JsonSerializer.SerializeToNode(model);
return node?.AsObject() ?? throw new
NullReferenceException("Failed to serialize state to JSON object.");
}
///
/// Deserialize a model from a JSON object.
///
protected static T DeserializeModel(JsonObject state)
{
return state.Deserialize() ?? throw new
NullReferenceException("Failed to deserialize state from JSON object.");
}
}