feat: added type container serialization

This commit is contained in:
Syntriax 2025-04-20 00:08:08 +03:00
parent 680d718957
commit 5bcc256777
2 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,10 @@
namespace Syntriax.Engine.Serialization;
public class TypeContainer
{
public string Type { get; set; } = string.Empty;
public TypeContainer() { }
public TypeContainer(Type type) { Type = type.FullName ?? string.Empty; }
public TypeContainer(object? value) { Type = value?.GetType().FullName ?? string.Empty; }
}

View File

@ -0,0 +1,27 @@
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
namespace Syntriax.Engine.Serialization;
public class TypeContainerConverter : IEngineTypeYamlConverter
{
public bool Accepts(Type type) => type == typeof(TypeContainer);
public object? ReadYaml(IParser parser, Type type, ObjectDeserializer rootDeserializer)
{
if (parser.Consume<Scalar>().Value.CompareTo(nameof(TypeContainer.Type)) != 0)
throw new ArgumentException($"{nameof(TypeContainer)} mapping must start with {nameof(TypeContainer.Type)}");
string typeFullName = parser.Consume<Scalar>().Value;
return new TypeContainer() { Type = typeFullName };
}
public void WriteYaml(IEmitter emitter, object? value, Type type, ObjectSerializer serializer)
{
TypeContainer? typeContainer = (TypeContainer)value!;
emitter.Emit(new Scalar(nameof(TypeContainer.Type)));
emitter.Emit(new Scalar(typeContainer.Type));
}
}