28 lines
992 B
C#
28 lines
992 B
C#
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));
|
|
}
|
|
}
|