feat: added type container serialization
This commit is contained in:
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user