Добавлен Core
This commit is contained in:
31
Lattice.Core/Persistence/LayoutJsonConverter.cs
Normal file
31
Lattice.Core/Persistence/LayoutJsonConverter.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user