Files
Lattice/Lattice.Core/Persistence/LayoutJsonConverter.cs
2026-01-07 21:31:57 +03:00

32 lines
1.3 KiB
C#

using Lattice.Core.Models;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Lattice.Core.Persistence;
/// <summary>
/// Конвертер для полиморфной сериализации и десериализации узлов дерева Lattice.
/// </summary>
public class LayoutJsonConverter : JsonConverter<LayoutNode>
{
public override LayoutNode? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
using var jsonDoc = JsonDocument.ParseValue(ref reader);
var rootElement = jsonDoc.RootElement;
// Определяем тип узла по наличию специфических свойств
if (rootElement.TryGetProperty("Orientation", out _))
{
return JsonSerializer.Deserialize<SplitContainerNode>(rootElement.GetRawText(), options);
}
return JsonSerializer.Deserialize<ContentNode>(rootElement.GetRawText(), options);
}
public override void Write(Utf8JsonWriter writer, LayoutNode value, JsonSerializerOptions options)
{
// Используем стандартную сериализацию для конкретных типов
JsonSerializer.Serialize(writer, (object)value, value.GetType(), options);
}
}