feat: added type container serialization
This commit is contained in:
parent
680d718957
commit
5bcc256777
10
Engine.Serialization/Converters/TypeContainer.cs
Normal file
10
Engine.Serialization/Converters/TypeContainer.cs
Normal 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; }
|
||||
}
|
27
Engine.Serialization/Converters/TypeContainerConverter.cs
Normal file
27
Engine.Serialization/Converters/TypeContainerConverter.cs
Normal 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));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user