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