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.
35 lines
1.0 KiB
35 lines
1.0 KiB
1 year ago
|
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
|
||
|
{
|
||
|
/// <inheritdoc />
|
||
|
public abstract void LoadStateFromJsonObject(JsonObject state);
|
||
|
|
||
|
/// <inheritdoc />
|
||
|
public abstract JsonObject SaveStateToJsonObject();
|
||
|
|
||
|
/// <summary>
|
||
|
/// Serialize a model to a JSON object.
|
||
|
/// </summary>
|
||
|
protected static JsonObject SerializeModel<T>(T model)
|
||
|
{
|
||
|
var node = JsonSerializer.SerializeToNode(model);
|
||
|
return node?.AsObject() ?? throw new
|
||
|
NullReferenceException("Failed to serialize state to JSON object.");
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Deserialize a model from a JSON object.
|
||
|
/// </summary>
|
||
|
protected static T DeserializeModel<T>(JsonObject state)
|
||
|
{
|
||
|
return state.Deserialize<T>() ?? throw new
|
||
|
NullReferenceException("Failed to deserialize state from JSON object.");
|
||
|
}
|
||
|
}
|